Skip to content

bytes::BufMut implementation to count buffer size

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

dtolnay/bufsize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

About

bytes::BufMut implementation to count buffer size

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Languages