13 releases (stable)

new 1.0.8 Nov 13, 2024
1.0.7 Dec 20, 2023
1.0.6 Jul 15, 2023
1.0.5 Mar 3, 2023
0.4.0 Oct 9, 2019

#38 in Memory management

Download history 1278/week @ 2024-07-27 1245/week @ 2024-08-03 1138/week @ 2024-08-10 1188/week @ 2024-08-17 961/week @ 2024-08-24 1083/week @ 2024-08-31 863/week @ 2024-09-07 869/week @ 2024-09-14 1323/week @ 2024-09-21 1962/week @ 2024-09-28 1385/week @ 2024-10-05 1685/week @ 2024-10-12 1971/week @ 2024-10-19 1227/week @ 2024-10-26 1198/week @ 2024-11-02 1044/week @ 2024-11-09

5,726 downloads per month
Used in 50 crates (3 directly)

MIT/Apache

11KB
207 lines

bufsize::SizeCounter

github crates.io docs.rs build status

This library provides an implementation of bytes::BufMut to count the size of a resulting buffer.

[dependencies]
bufsize = "1.0"

Compiler support: requires rustc 1.39+


Example

use bufsize::SizeCounter;
use bytes::BufMut;

pub struct DataStructure;

impl DataStructure {
    pub fn serialize<B: BufMut>(&self, buf: &mut B) {
        let name = "DataStructure";
        buf.put_u8(name.len() as u8);
        buf.put_slice(name.as_bytes());
        buf.put_u32_le(9999);
        buf.put_f32_le(1.0);
    }
}

fn main() {
    let mut sizecount = SizeCounter::new();
    DataStructure.serialize(&mut sizecount);

    let mut buffer = Vec::with_capacity(sizecount.size());
    DataStructure.serialize(&mut buffer);

    assert_eq!(sizecount.size(), buffer.len());
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~175KB