scroll-buffer

Crates.ioscroll-buffer
lib.rsscroll-buffer
version0.3.1
sourcesrc
created_at2022-01-03 00:08:06.999803
updated_at2023-12-15 23:12:28.111167
descriptionExtra writable buffers for Scroll
homepage
repositoryhttps://github.com/nickbclifford/scroll-buffer
max_upload_size
id506802
size16,077
Nick Clifford (nickbclifford)

documentation

README

scroll-buffer

Cargo Crates.io

Additional Pwrite buffers for use with Scroll.

Documentation

Docs are available at docs.rs.

Overview

Since Scroll's writing traits are generic over their destination buffer, if you find the default mutable byte slice too restrictive, you can use the types in this crate for more powerful features like dynamic allocation.

Currently, this crate only defines one such buffer: the DynamicBuffer type, which grows on-demand when writing types into it.

Usage

Add to your Cargo.toml:

[dependencies]
scroll-buffer = "0.3.0"

You can now change your TryIntoCtx implementations and start writing!

use scroll::{LE, Pwrite, ctx::TryIntoCtx};
use scroll_buffer::DynamicBuffer;

fn main() -> Result<(), scroll::Error> {
    let mut buf = DynamicBuffer::new();
    
    // works with regular ints, of course...
    buf.pwrite_with(0xbeefu16, 0, LE)?;
    assert_eq!(buf.get(), [0xef, 0xbe]);
    
    // ...but also custom types!
    struct MyCoolStruct(bool, u32);
    impl TryIntoCtx<(), DynamicBuffer> for MyCoolStruct {
        type Error = scroll::Error;

        fn try_into_ctx(self, buf: &mut DynamicBuffer, _: ()) -> Result<usize, Self::Error> {
            let offset = &mut 0;
            
            if self.0 {
                buf.gwrite_with(0xffu8, offset, LE)?;
            } else {
                buf.gwrite_with(self.1, offset, LE)?;
            }
            
            Ok(*offset)
        }
    }
    
    buf.clear();
    buf.pwrite(MyCoolStruct(false, 1), 0)?;
    assert_eq!(buf.get(), [0x01, 0x00, 0x00, 0x00]);
    
    buf.pwrite(MyCoolStruct(true, 0), 4)?;
    assert_eq!(buf.get(), [0x01, 0x00, 0x00, 0x00, 0xff]);
    
    Ok(())
}
Commit count: 13

cargo fmt