Skip to content

Commit

Permalink
feat: bits_for*()
Browse files Browse the repository at this point in the history
  • Loading branch information
mitinarseny committed Aug 31, 2024
1 parent 13d01f7 commit ce2b178
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 26 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion crates/bits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
30 changes: 29 additions & 1 deletion crates/bits/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -49,6 +51,32 @@ where
Ok(writer)
}

#[inline]
pub fn bits_for<T>(value: T) -> Result<usize, StringError>
where
T: BitPack,
{
bits_for_as::<_, Same>(value)
}

#[inline]
pub fn bits_for_as<T, As>(value: T) -> Result<usize, StringError>
where
As: BitPackAs<T>,
{
bits_for_as_with::<_, NoArgs<_, As>>(value, ())
}

#[inline]
pub fn bits_for_as_with<T, As>(value: T, args: As::Args) -> Result<usize, StringError>
where
As: BitPackAsWithArgs<T>,
{
let mut writer = NoopBitWriter.counted();
BitWriterExt::pack_as_with::<_, As>(&mut writer, value, args)?;
Ok(writer.bit_count())
}

impl BitPack for () {
#[inline]
fn pack<W>(&self, _writer: W) -> Result<(), W::Error>
Expand Down
3 changes: 2 additions & 1 deletion crates/bits/src/ser/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ pub trait BitWriterExt: BitWriter {
}
impl<T> BitWriterExt for T where T: BitWriter {}

struct NoopBitWriter;
#[derive(Debug, Clone, Copy)]
pub struct NoopBitWriter;

impl BitWriter for NoopBitWriter {
type Error = StringError;
Expand Down
2 changes: 1 addition & 1 deletion crates/contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/tlb-ton/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 10 additions & 9 deletions crates/tlb-ton/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<EitherInlineOrRef>>(&self.init)?
// body:(Either X ^X)
.store_as::<_, EitherInlineOrRef>(&self.body)?;
Ok(())
}
Expand All @@ -91,15 +93,14 @@ where
{
fn parse(parser: &mut CellParser<'de>) -> Result<Self, CellParserError<'de>> {
Ok(Self {
// info:CommonMsgInfo
info: parser.parse().context("info")?,
// init:(Maybe (Either StateInit ^StateInit))
init: parser
.parse_as::<_, Option<Either<Same, Ref>>>()
.context("init")?
.map(Either::into_inner),
body: parser
.parse_as::<Either<T, T>, Either<Same, Ref>>()
.context("body")?
.into_inner(),
.parse_as::<_, Option<EitherInlineOrRef>>()
.context("init")?,
// body:(Either X ^X)
body: parser.parse_as::<_, EitherInlineOrRef>().context("body")?,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tlb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/toner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ce2b178

Please sign in to comment.