Skip to content

Commit

Permalink
move serialized_length functions into their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Dec 31, 2024
1 parent 9a0ed3a commit 34658e4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ mod ser;
mod ser_br;
mod tools;
mod utils;
mod serialized_length;
pub mod write_atom;

#[cfg(test)]
mod test;

pub use serialized_length::{serialized_length_atom, serialized_length_small_number};
pub use de::node_from_bytes;
pub use de_br::{node_from_bytes_backrefs, node_from_bytes_backrefs_record};
pub use de_tree::{parse_triples, ParsedTriple};
Expand Down
16 changes: 2 additions & 14 deletions src/serde/object_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::allocator::{Allocator, NodePtr, SExp};
use std::collections::HashMap;
type CachedFunction<T> = fn(&mut ObjectCache<T>, &Allocator, NodePtr) -> Option<T>;
use super::bytes32::{hash_blobs, Bytes32};
use crate::serde::serialized_length_atom;

pub struct ObjectCache<T> {
cache: HashMap<NodePtr, T>,
Expand Down Expand Up @@ -130,20 +131,7 @@ pub fn serialized_length(
},
SExp::Atom => {
let buf = allocator.atom(node);
let lb: u64 = buf.as_ref().len().try_into().unwrap_or(u64::MAX);
Some(if lb == 0 || (lb == 1 && buf.as_ref()[0] < 128) {
1
} else if lb < 0x40 {
1 + lb
} else if lb < 0x2000 {
2 + lb
} else if lb < 0x100000 {
3 + lb
} else if lb < 0x8000000 {
4 + lb
} else {
5 + lb
})
Some(serialized_length_atom(buf.as_ref()) as u64)
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/serde/serialized_length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::allocator::len_for_value;

pub fn serialized_length_atom(buf: &[u8]) -> u32 {
let lb = buf.len() as u32;
if lb == 0 || (lb == 1 && buf[0] < 128) {
1
} else if lb < 0x40 {
1 + lb
} else if lb < 0x2000 {
2 + lb
} else if lb < 0x100000 {
3 + lb
} else if lb < 0x8000000 {
4 + lb
} else {
5 + lb
}
}

pub fn serialized_length_small_number(val: u32) -> u32 {
len_for_value(val) as u32 + 1
}

#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;

#[rstest]
#[case(&[], 1)]
#[case(&[1], 1)]
#[case(&[0x7f], 1)]
#[case(&[0x80], 2)]
#[case(&[0x81], 2)]
#[case(&[0x80, 0], 3)]
#[case(&[1; 0x3f], 0x40)]
#[case(&[1; 0x40], 0x42)]
fn test_serialized_length_atom(#[case] atom: &[u8], #[case] expect: u32) {
assert_eq!(serialized_length_atom(atom), expect);
}

#[rstest]
#[case(0, 1)]
#[case(1, 2)]
#[case(0x7f, 2)]
#[case(0x80, 3)]
#[case(0x7fff, 3)]
#[case(0x7fffff, 4)]
#[case(0x800000, 5)]
#[case(0x7fffffff, 5)]
#[case(0x80000000, 6)]
#[case(0xffffffff, 6)]
fn test_serialized_length_small_number(#[case] value: u32, #[case] expect: u32) {
assert_eq!(serialized_length_small_number(value), expect);
}
}

0 comments on commit 34658e4

Please sign in to comment.