| 1 |
use core::result; |
| 2 |
|
| 3 |
use crate::ctx::TryIntoCtx; |
| 4 |
use crate::error; |
| 5 |
|
| 6 |
/// A very generic, contextual pwrite interface in Rust. |
| 7 |
/// |
| 8 |
/// Like [Pread](trait.Pread.html) — but for writing! |
| 9 |
/// |
| 10 |
/// Implementing `Pwrite` on a data store allows you to then write almost arbitrarily complex types |
| 11 |
/// efficiently. |
| 12 |
/// |
| 13 |
/// To this end the Pwrite trait works in conjuction with the [TryIntoCtx](ctx/trait.TryIntoCtx.html); |
| 14 |
/// The `TryIntoCtx` trait implemented on a type defines how to convert said type into data that |
| 15 |
/// an implementation of Pwrite can … well … write. |
| 16 |
/// |
| 17 |
/// As with [Pread](trait.Pread.html) 'data' does not necessarily mean `&[u8]` but can be any |
| 18 |
/// indexable type. In fact much of the documentation of `Pread` applies to `Pwrite` as well just |
| 19 |
/// with 'read' switched for 'write' and 'From' switched with 'Into' so if you haven't yet you |
| 20 |
/// should read the documentation of `Pread` first. |
| 21 |
/// |
| 22 |
/// As with `Pread`, note that in the particular case of the implementation of `Pwrite` for `[u8]`, |
| 23 |
/// writing it at the length boundary of that slice will cause to write in an empty slice. |
| 24 |
/// i.e. we make use of the fact that `&bytes[bytes.len()..]` will return an empty slice, rather |
| 25 |
/// than returning an error. In the past, scroll returned an offset error. |
| 26 |
/// In this case, this is relevant if you are writing an empty slice inside an empty slice and |
| 27 |
/// expected this to work. |
| 28 |
/// |
| 29 |
/// Unless you need to implement your own data store — that is either can't convert to `&[u8]` or |
| 30 |
/// have a data that does not expose a `&mut [u8]` — you will probably want to implement |
| 31 |
/// [TryIntoCtx](ctx/trait.TryIntoCtx.html) on your Rust types to be written. |
| 32 |
/// |
| 33 |
pub trait Pwrite<Ctx: Copy, E> { |
| 34 |
#[inline] |
| 35 |
fn pwrite<N: TryIntoCtx<Ctx, Self, Error = E>>( |
| 36 |
&mut self, |
| 37 |
n: N, |
| 38 |
offset: usize, |
| 39 |
) -> result::Result<usize, E> |
| 40 |
where |
| 41 |
Ctx: Default, |
| 42 |
{ |
| 43 |
self.pwrite_with(n, offset, Ctx::default()) |
| 44 |
} |
| 45 |
|
| 46 |
/// Write `N` at offset `I` with context `Ctx` |
| 47 |
/// # Example |
| 48 |
/// ``` |
| 49 |
/// use scroll::{Pwrite, Pread, LE}; |
| 50 |
/// let mut bytes: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0]; |
| 51 |
/// bytes.pwrite_with::<u32>(0xbeefbeef, 0, LE).unwrap(); |
| 52 |
/// assert_eq!(bytes.pread_with::<u32>(0, LE).unwrap(), 0xbeefbeef); |
| 53 |
fn pwrite_with<N: TryIntoCtx<Ctx, Self, Error = E>>( |
| 54 |
&mut self, |
| 55 |
n: N, |
| 56 |
offset: usize, |
| 57 |
ctx: Ctx, |
| 58 |
) -> result::Result<usize, E>; |
| 59 |
|
| 60 |
/// Write `n` into `self` at `offset`, with a default `Ctx`. Updates the offset. |
| 61 |
#[inline] |
| 62 |
fn gwrite<N: TryIntoCtx<Ctx, Self, Error = E>>( |
| 63 |
&mut self, |
| 64 |
n: N, |
| 65 |
offset: &mut usize, |
| 66 |
) -> result::Result<usize, E> |
| 67 |
where |
| 68 |
Ctx: Default, |
| 69 |
{ |
| 70 |
let ctx = Ctx::default(); |
| 71 |
self.gwrite_with(n, offset, ctx) |
| 72 |
} |
| 73 |
|
| 74 |
/// Write `n` into `self` at `offset`, with the `ctx`. Updates the offset. |
| 75 |
#[inline] |
| 76 |
fn gwrite_with<N: TryIntoCtx<Ctx, Self, Error = E>>( |
| 77 |
&mut self, |
| 78 |
n: N, |
| 79 |
offset: &mut usize, |
| 80 |
ctx: Ctx, |
| 81 |
) -> result::Result<usize, E> { |
| 82 |
let o = *offset; |
| 83 |
self.pwrite_with(n, o, ctx).map(|size| { |
| 84 |
*offset += size; |
| 85 |
size |
| 86 |
}) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl<Ctx: Copy, E: From<error::Error>> Pwrite<Ctx, E> for [u8] { |
| 91 |
fn pwrite_with<N: TryIntoCtx<Ctx, Self, Error = E>>( |
| 92 |
&mut self, |
| 93 |
n: N, |
| 94 |
offset: usize, |
| 95 |
ctx: Ctx, |
| 96 |
) -> result::Result<usize, E> { |
| 97 |
if offset > self.len() { |
| 98 |
return Err(error::Error::BadOffset(offset).into()); |
| 99 |
} |
| 100 |
let dst = &mut self[offset..]; |
| 101 |
n.try_into_ctx(dst, ctx) |
| 102 |
} |
| 103 |
} |