#bytes

bufsize

bytes::BufMut implementation to count buffer size

10 releases (6 stable)

1.0.5 Mar 3, 2023
1.0.4 Dec 17, 2022
1.0.3 Aug 3, 2022
1.0.1 Apr 30, 2022
0.4.0 Oct 9, 2019

#26 in Memory management

Download history 6836/week @ 2022-11-29 5986/week @ 2022-12-06 6269/week @ 2022-12-13 2661/week @ 2022-12-20 1858/week @ 2022-12-27 5098/week @ 2023-01-03 6146/week @ 2023-01-10 7982/week @ 2023-01-17 9034/week @ 2023-01-24 9471/week @ 2023-01-31 2879/week @ 2023-02-07 1610/week @ 2023-02-14 1429/week @ 2023-02-21 776/week @ 2023-02-28 420/week @ 2023-03-07 508/week @ 2023-03-14

3,231 downloads per month
Used in 45 crates (2 directly)

MIT/Apache

9KB
177 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

~170KB