diff --git a/Cargo.lock b/Cargo.lock index dd25c84..5848bc3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -692,7 +692,7 @@ dependencies = [ [[package]] name = "tlb" -version = "0.3.1" +version = "0.3.2" dependencies = [ "bitvec", "hex", @@ -705,7 +705,7 @@ dependencies = [ [[package]] name = "tlb-ton" -version = "0.3.1" +version = "0.3.2" dependencies = [ "base64 0.21.7", "chrono", @@ -723,7 +723,7 @@ dependencies = [ [[package]] name = "tlbits" -version = "0.3.1" +version = "0.3.2" dependencies = [ "bitvec", "either", @@ -735,7 +735,7 @@ dependencies = [ [[package]] name = "ton-contracts" -version = "0.3.1" +version = "0.3.2" dependencies = [ "anyhow", "bitvec", @@ -753,7 +753,7 @@ dependencies = [ [[package]] name = "toner" -version = "0.3.1" +version = "0.3.2" dependencies = [ "tlb", "tlb-ton", diff --git a/Cargo.toml b/Cargo.toml index c296921..4cca34e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,11 +11,11 @@ categories = ["encoding"] license-file = "LICENSE.txt" [workspace.dependencies] -tlb = { path = "./crates/tlb", version = "0.3.1" } -tlbits = { path = "./crates/bits", version = "0.3.1" } -tlb-ton = { path = "./crates/tlb-ton", version = "0.3.1" } -ton-contracts = { path = "./crates/contracts", version = "0.3.1" } -toner = { path = "./crates/toner", version = "0.3.1" } +tlb = { path = "./crates/tlb", version = "0.3.2" } +tlbits = { path = "./crates/bits", version = "0.3.2" } +tlb-ton = { path = "./crates/tlb-ton", version = "0.3.2" } +ton-contracts = { path = "./crates/contracts", version = "0.3.2" } +toner = { path = "./crates/toner", version = "0.3.2" } anyhow = "1" base64 = "0.21" diff --git a/crates/bits/Cargo.toml b/crates/bits/Cargo.toml index 90f5000..505f8c1 100644 --- a/crates/bits/Cargo.toml +++ b/crates/bits/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tlbits" -version = "0.3.1" +version = "0.3.2" edition.workspace = true repository.workspace = true license-file.workspace = true diff --git a/crates/bits/src/ser/mod.rs b/crates/bits/src/ser/mod.rs index c4566da..b972fe5 100644 --- a/crates/bits/src/ser/mod.rs +++ b/crates/bits/src/ser/mod.rs @@ -7,12 +7,14 @@ pub use self::writer::*; use std::{rc::Rc, sync::Arc}; +use args::r#as::BitPackAsWithArgs; use bitvec::{order::Msb0, slice::BitSlice, vec::BitVec}; use either::Either; use impl_tools::autoimpl; +use r#as::BitPackAs; use crate::{ - r#as::{AsBytes, Same}, + r#as::{args::NoArgs, AsBytes, Same}, ResultExt, StringError, }; @@ -49,6 +51,32 @@ where Ok(writer) } +#[inline] +pub fn bits_for(value: T) -> Result +where + T: BitPack, +{ + bits_for_as::<_, Same>(value) +} + +#[inline] +pub fn bits_for_as(value: T) -> Result +where + As: BitPackAs, +{ + bits_for_as_with::<_, NoArgs<_, As>>(value, ()) +} + +#[inline] +pub fn bits_for_as_with(value: T, args: As::Args) -> Result +where + As: BitPackAsWithArgs, +{ + let mut writer = NoopBitWriter.counted(); + BitWriterExt::pack_as_with::<_, As>(&mut writer, value, args)?; + Ok(writer.bit_count()) +} + impl BitPack for () { #[inline] fn pack(&self, _writer: W) -> Result<(), W::Error> diff --git a/crates/bits/src/ser/writer.rs b/crates/bits/src/ser/writer.rs index 96a2e1b..3b9fe00 100644 --- a/crates/bits/src/ser/writer.rs +++ b/crates/bits/src/ser/writer.rs @@ -219,7 +219,8 @@ pub trait BitWriterExt: BitWriter { } impl BitWriterExt for T where T: BitWriter {} -struct NoopBitWriter; +#[derive(Debug, Clone, Copy)] +pub struct NoopBitWriter; impl BitWriter for NoopBitWriter { type Error = StringError; diff --git a/crates/contracts/Cargo.toml b/crates/contracts/Cargo.toml index e378779..3a0c4de 100644 --- a/crates/contracts/Cargo.toml +++ b/crates/contracts/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ton-contracts" -version = "0.3.1" +version = "0.3.2" edition.workspace = true repository.workspace = true license-file.workspace = true diff --git a/crates/tlb-ton/Cargo.toml b/crates/tlb-ton/Cargo.toml index 37ec891..5f892f9 100644 --- a/crates/tlb-ton/Cargo.toml +++ b/crates/tlb-ton/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tlb-ton" -version = "0.3.1" +version = "0.3.2" edition.workspace = true repository.workspace = true license-file.workspace = true diff --git a/crates/tlb-ton/src/message.rs b/crates/tlb-ton/src/message.rs index 3781bfb..13d5c0a 100644 --- a/crates/tlb-ton/src/message.rs +++ b/crates/tlb-ton/src/message.rs @@ -8,8 +8,7 @@ use tlb::{ ser::{BitPack, BitWriter, BitWriterExt}, }, de::{CellDeserialize, CellParser, CellParserError}, - either::Either, - r#as::{DefaultOnNone, EitherInlineOrRef, Ref, Same}, + r#as::{DefaultOnNone, EitherInlineOrRef}, ser::{CellBuilder, CellBuilderError, CellSerialize, CellSerializeExt}, Cell, ResultExt, }; @@ -76,8 +75,11 @@ where { fn store(&self, builder: &mut CellBuilder) -> Result<(), CellBuilderError> { builder + // info:CommonMsgInfo .store(&self.info)? + // init:(Maybe (Either StateInit ^StateInit)) .store_as::<_, &Option>(&self.init)? + // body:(Either X ^X) .store_as::<_, EitherInlineOrRef>(&self.body)?; Ok(()) } @@ -91,15 +93,14 @@ where { fn parse(parser: &mut CellParser<'de>) -> Result> { Ok(Self { + // info:CommonMsgInfo info: parser.parse().context("info")?, + // init:(Maybe (Either StateInit ^StateInit)) init: parser - .parse_as::<_, Option>>() - .context("init")? - .map(Either::into_inner), - body: parser - .parse_as::, Either>() - .context("body")? - .into_inner(), + .parse_as::<_, Option>() + .context("init")?, + // body:(Either X ^X) + body: parser.parse_as::<_, EitherInlineOrRef>().context("body")?, }) } } diff --git a/crates/tlb/Cargo.toml b/crates/tlb/Cargo.toml index 1b7e20e..1130e43 100644 --- a/crates/tlb/Cargo.toml +++ b/crates/tlb/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tlb" -version = "0.3.1" +version = "0.3.2" edition.workspace = true repository.workspace = true license-file.workspace = true diff --git a/crates/toner/Cargo.toml b/crates/toner/Cargo.toml index 51544c8..d459539 100644 --- a/crates/toner/Cargo.toml +++ b/crates/toner/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "toner" -version = "0.3.1" +version = "0.3.2" edition.workspace = true repository.workspace = true license-file.workspace = true