Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions rust/derive/tests/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn enum_custom_tags() -> common::Result {
Five {},
}

impl StrictSerialize for Assoc {}
impl StrictSerialize<256> for Assoc {}

assert_eq!(Assoc::ALL_VARIANTS, &[
(0, "one"),
Expand All @@ -205,13 +205,13 @@ fn enum_custom_tags() -> common::Result {
]);

let assoc = Assoc::Two(0, 1, 2);
assert_eq!(assoc.to_strict_serialized::<256>().unwrap().as_slice(), &[2, 0, 1, 0, 2, 0, 0, 0]);
assert_eq!(assoc.to_strict_vec().unwrap().as_slice(), &[2, 0, 1, 0, 2, 0, 0, 0]);

let assoc = Assoc::One {
hash: [0u8; 32],
ord: 0,
};
assert_eq!(assoc.to_strict_serialized::<256>().unwrap().as_slice(), &[0u8; 34]);
assert_eq!(assoc.to_strict_vec().unwrap().as_slice(), &[0u8; 34]);

Ok(())
}
2 changes: 1 addition & 1 deletion rust/derive/tests/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn skip_field() -> common::Result {
must_camelize: 2,
wrong_name: 3,
};
assert_eq!(val.to_strict_serialized::<{ usize::MAX }>().unwrap().as_slice(), &[2]);
assert_eq!(val.to_strict_vec().unwrap().as_slice(), &[2]);
let val = Struct {
must_camelize: 2,
wrong_name: 0,
Expand Down
52 changes: 36 additions & 16 deletions rust/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,34 +381,54 @@ impl<T> StrictDecode for PhantomData<T> {
fn strict_decode(_reader: &mut impl TypedRead) -> Result<Self, DecodeError> { Ok(default!()) }
}

// TODO: Provide max length as a trait-level const
pub trait StrictSerialize: StrictEncode {
fn strict_serialized_len<const MAX: usize>(&self) -> io::Result<usize> {
pub trait StrictSerialize<const MAX_SERIALIZED_LEN: usize>: StrictEncode {
fn strict_serialize(&self, write: impl io::Write) -> Result<(), io::Error> {
let writer = StreamWriter::new::<MAX_SERIALIZED_LEN>(write);
self.strict_write(writer)
}

fn to_strict_vec(&self) -> Result<Confined<Vec<u8>, 0, MAX_SERIALIZED_LEN>, SerializeError> {
let ast_data = StrictWriter::in_memory::<MAX_SERIALIZED_LEN>();
let data = self.strict_encode(ast_data)?.unbox().unconfine();
Confined::<Vec<u8>, 0, MAX_SERIALIZED_LEN>::try_from(data).map_err(SerializeError::from)
}

//#[cfg(feature = "std")]
fn strict_serialize_to_path(
&self,
path: impl AsRef<std::path::Path>,
overwrite: bool,
) -> Result<(), SerializeError> {
let file = if overwrite { fs::File::create(path)? } else { fs::File::create_new(path)? };
self.strict_serialize(file)?;
Ok(())
}

#[deprecated(since = "3.0.0", note = "use `StrictEncode::strict_len` instead")]
fn strict_serialized_len<const MAX: usize>(&self) -> io::Result<usize>
where Self: StrictSerialize<MAX> {
let counter = StrictWriter::counter::<MAX>();
Ok(self.strict_encode(counter)?.unbox().unconfine().count)
}

#[deprecated(since = "3.0.0", note = "use `to_strict_vec` instead")]
fn to_strict_serialized<const MAX: usize>(
&self,
) -> Result<Confined<Vec<u8>, 0, MAX>, SerializeError> {
let ast_data = StrictWriter::in_memory::<MAX>();
let data = self.strict_encode(ast_data)?.unbox().unconfine();
Confined::<Vec<u8>, 0, MAX>::try_from(data).map_err(SerializeError::from)
}

fn strict_serialize<const MAX: usize>(&self, write: impl io::Write) -> Result<(), io::Error> {
let writer = StreamWriter::new::<MAX>(write);
self.strict_write(writer)
) -> Result<Confined<Vec<u8>, 0, MAX>, SerializeError>
where Self: StrictSerialize<MAX> {
self.to_strict_vec()
}

#[deprecated(since = "3.0.0", note = "use `strict_serialize_to_path` instead")]
fn strict_serialize_to_file<const MAX: usize>(
&self,
path: impl AsRef<std::path::Path>,
) -> Result<(), SerializeError> {
) -> Result<(), SerializeError>
where
Self: StrictSerialize<MAX>,
{
let file = fs::File::create(path)?;
// TODO: Do FileWriter
let file = StrictWriter::with(StreamWriter::new::<MAX>(file));
self.strict_encode(file)?;
StrictSerialize::<MAX_SERIALIZED_LEN>::strict_serialize(self, file)?;
Ok(())
}
}
Expand Down
Loading