smallbytes 0.1.0

SmallBytes = SmallVec + impl BufMut (from the bytes crate)
Documentation
  • Coverage
  • 100%
    4 out of 4 items documented3 out of 4 items with examples
  • Size
  • Source code size: 9.68 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.15 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • orottier

Smallbytes

crates.io docs.rs

SmallBytes = SmallVec + impl BufMut (from the bytes crate)

use smallbytes::SmallBytes;
use bytes::BufMut;

// initialize a buffer with inline capacity of 6 bytes
let mut buf = SmallBytes::<6>::new();

// the first word fits inline (stack)
buf.put(&b"hello"[..]);

// the rest does not, so the contents are moved to the heap
buf.put(&b" world"[..]);
buf.put_u16(1234);

assert_eq!(buf.as_ref(), &b"hello world\x04\xD2"[..]);

The size of a SmallBytes object is at least 24 bytes (pointer, length, capacity) similar to a Vec. This means you can always store 16 bytes on the stack for free.

use std::mem::size_of;
use smallbytes::SmallBytes;

assert_eq!(24, size_of::<SmallBytes<0>>());  // zero bytes on the stack, don't do this
assert_eq!(24, size_of::<SmallBytes<8>>());  // 8 bytes on the stack
assert_eq!(24, size_of::<SmallBytes<16>>()); // 16 bytes on the stack (ideal minimum)
assert_eq!(32, size_of::<SmallBytes<24>>()); // 24 bytes on the stack (stack size increases)