Trait scroll::Pwrite

source ·
pub trait Pwrite<Ctx: Copy, E> {
    // Required method
    fn pwrite_with<N: TryIntoCtx<Ctx, Self, Error = E>>(
        &mut self,
        n: N,
        offset: usize,
        ctx: Ctx
    ) -> Result<usize, E>;

    // Provided methods
    fn pwrite<N: TryIntoCtx<Ctx, Self, Error = E>>(
        &mut self,
        n: N,
        offset: usize
    ) -> Result<usize, E>
       where Ctx: Default { ... }
    fn gwrite<N: TryIntoCtx<Ctx, Self, Error = E>>(
        &mut self,
        n: N,
        offset: &mut usize
    ) -> Result<usize, E>
       where Ctx: Default { ... }
    fn gwrite_with<N: TryIntoCtx<Ctx, Self, Error = E>>(
        &mut self,
        n: N,
        offset: &mut usize,
        ctx: Ctx
    ) -> Result<usize, E> { ... }
}
Expand description

A very generic, contextual pwrite interface in Rust.

Like Pread — but for writing!

Implementing Pwrite on a data store allows you to then write almost arbitrarily complex types efficiently.

To this end the Pwrite trait works in conjuction with the TryIntoCtx; The TryIntoCtx trait implemented on a type defines how to convert said type into data that an implementation of Pwrite can … well … write.

As with Pread ‘data’ does not necessarily mean &[u8] but can be any indexable type. In fact much of the documentation of Pread applies to Pwrite as well just with ‘read’ switched for ‘write’ and ‘From’ switched with ‘Into’ so if you haven’t yet you should read the documentation of Pread first.

As with Pread, note that in the particular case of the implementation of Pwrite for [u8], writing it at the length boundary of that slice will cause to write in an empty slice. i.e. we make use of the fact that &bytes[bytes.len()..] will return an empty slice, rather than returning an error. In the past, scroll returned an offset error. In this case, this is relevant if you are writing an empty slice inside an empty slice and expected this to work.

Unless you need to implement your own data store — that is either can’t convert to &[u8] or have a data that does not expose a &mut [u8] — you will probably want to implement TryIntoCtx on your Rust types to be written.

Required Methods§

source

fn pwrite_with<N: TryIntoCtx<Ctx, Self, Error = E>>( &mut self, n: N, offset: usize, ctx: Ctx ) -> Result<usize, E>

Write N at offset I with context Ctx

Example
use scroll::{Pwrite, Pread, LE};
let mut bytes: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
bytes.pwrite_with::<u32>(0xbeefbeef, 0, LE).unwrap();
assert_eq!(bytes.pread_with::<u32>(0, LE).unwrap(), 0xbeefbeef);

Provided Methods§

source

fn pwrite<N: TryIntoCtx<Ctx, Self, Error = E>>( &mut self, n: N, offset: usize ) -> Result<usize, E>
where Ctx: Default,

source

fn gwrite<N: TryIntoCtx<Ctx, Self, Error = E>>( &mut self, n: N, offset: &mut usize ) -> Result<usize, E>
where Ctx: Default,

Write n into self at offset, with a default Ctx. Updates the offset.

source

fn gwrite_with<N: TryIntoCtx<Ctx, Self, Error = E>>( &mut self, n: N, offset: &mut usize, ctx: Ctx ) -> Result<usize, E>

Write n into self at offset, with the ctx. Updates the offset.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<Ctx: Copy, E: From<Error>> Pwrite<Ctx, E> for [u8]

source§

fn pwrite_with<N: TryIntoCtx<Ctx, Self, Error = E>>( &mut self, n: N, offset: usize, ctx: Ctx ) -> Result<usize, E>

Implementors§