| 1 |
use core::ops::{Index, IndexMut, RangeFrom}; |
| 2 |
|
| 3 |
use crate::ctx::{FromCtx, IntoCtx}; |
| 4 |
|
| 5 |
/// Core-read - core, no_std friendly trait for reading basic traits from byte buffers. Cannot fail |
| 6 |
/// unless the buffer is too small, in which case an assert fires and the program panics. |
| 7 |
/// |
| 8 |
/// If your type implements [FromCtx](ctx/trait.FromCtx.html) then you can `cread::<YourType>(offset)`. |
| 9 |
/// |
| 10 |
/// # Example |
| 11 |
/// |
| 12 |
/// ```rust |
| 13 |
/// use scroll::{ctx, Cread, LE}; |
| 14 |
/// |
| 15 |
/// #[repr(packed)] |
| 16 |
/// struct Bar { |
| 17 |
/// foo: i32, |
| 18 |
/// bar: u32, |
| 19 |
/// } |
| 20 |
/// |
| 21 |
/// impl ctx::FromCtx<scroll::Endian> for Bar { |
| 22 |
/// fn from_ctx(bytes: &[u8], ctx: scroll::Endian) -> Self { |
| 23 |
/// use scroll::Cread; |
| 24 |
/// Bar { foo: bytes.cread_with(0, ctx), bar: bytes.cread_with(4, ctx) } |
| 25 |
/// } |
| 26 |
/// } |
| 27 |
/// |
| 28 |
/// let bytes = [0xff, 0xff, 0xff, 0xff, 0xef,0xbe,0xad,0xde,]; |
| 29 |
/// let bar = bytes.cread_with::<Bar>(0, LE); |
| 30 |
/// // Remember that you need to copy out fields from packed structs |
| 31 |
/// // with a `{}` block instead of borrowing them directly |
| 32 |
/// // ref: https://github.com/rust-lang/rust/issues/46043 |
| 33 |
/// assert_eq!({bar.foo}, -1); |
| 34 |
/// assert_eq!({bar.bar}, 0xdeadbeef); |
| 35 |
/// ``` |
| 36 |
pub trait Cread<Ctx, I = usize>: Index<I> + Index<RangeFrom<I>> |
| 37 |
where |
| 38 |
Ctx: Copy, |
| 39 |
{ |
| 40 |
/// Reads a value from `Self` at `offset` with `ctx`. Cannot fail. |
| 41 |
/// If the buffer is too small for the value requested, this will panic. |
| 42 |
/// |
| 43 |
/// # Example |
| 44 |
/// |
| 45 |
/// ```rust |
| 46 |
/// use scroll::{Cread, BE, LE}; |
| 47 |
/// use std::i64::MAX; |
| 48 |
/// |
| 49 |
/// let bytes = [0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xef,0xbe,0xad,0xde,]; |
| 50 |
/// let foo = bytes.cread_with::<i64>(0, BE); |
| 51 |
/// let bar = bytes.cread_with::<u32>(8, LE); |
| 52 |
/// assert_eq!(foo, MAX); |
| 53 |
/// assert_eq!(bar, 0xdeadbeef); |
| 54 |
/// ``` |
| 55 |
#[inline] |
| 56 |
fn cread_with<N: FromCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>( |
| 57 |
&self, |
| 58 |
offset: I, |
| 59 |
ctx: Ctx, |
| 60 |
) -> N { |
| 61 |
N::from_ctx(&self[offset..], ctx) |
| 62 |
} |
| 63 |
/// Reads a value implementing `FromCtx` from `Self` at `offset`, |
| 64 |
/// with the **target machine**'s endianness. |
| 65 |
/// For the primitive types, this will be the **target machine**'s endianness. |
| 66 |
/// |
| 67 |
/// # Example |
| 68 |
/// |
| 69 |
/// ```rust |
| 70 |
/// use scroll::Cread; |
| 71 |
/// |
| 72 |
/// let bytes = [0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xef,0xbe,0x00,0x00,]; |
| 73 |
/// let foo = bytes.cread::<i64>(0); |
| 74 |
/// let bar = bytes.cread::<u32>(8); |
| 75 |
/// #[cfg(target_endian = "little")] |
| 76 |
/// assert_eq!(foo, 1); |
| 77 |
/// #[cfg(target_endian = "big")] |
| 78 |
/// assert_eq!(foo, 0x100_0000_0000_0000); |
| 79 |
/// |
| 80 |
/// #[cfg(target_endian = "little")] |
| 81 |
/// assert_eq!(bar, 0xbeef); |
| 82 |
/// #[cfg(target_endian = "big")] |
| 83 |
/// assert_eq!(bar, 0xefbe0000); |
| 84 |
/// ``` |
| 85 |
#[inline] |
| 86 |
fn cread<N: FromCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>(&self, offset: I) -> N |
| 87 |
where |
| 88 |
Ctx: Default, |
| 89 |
{ |
| 90 |
let ctx = Ctx::default(); |
| 91 |
N::from_ctx(&self[offset..], ctx) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl<Ctx: Copy, I, R: ?Sized + Index<I> + Index<RangeFrom<I>>> Cread<Ctx, I> for R {} |
| 96 |
|
| 97 |
/// Core-write - core, no_std friendly trait for writing basic types into byte buffers. Cannot fail |
| 98 |
/// unless the buffer is too small, in which case an assert fires and the program panics. |
| 99 |
/// Similar to [Cread](trait.Cread.html), if your type implements [IntoCtx](ctx/trait.IntoCtx.html) |
| 100 |
/// then you can `cwrite(your_type, offset)`. |
| 101 |
/// |
| 102 |
/// # Example |
| 103 |
/// |
| 104 |
/// ```rust |
| 105 |
/// use scroll::{ctx, Cwrite}; |
| 106 |
/// |
| 107 |
/// #[repr(packed)] |
| 108 |
/// struct Bar { |
| 109 |
/// foo: i32, |
| 110 |
/// bar: u32, |
| 111 |
/// } |
| 112 |
/// |
| 113 |
/// impl ctx::IntoCtx<scroll::Endian> for Bar { |
| 114 |
/// fn into_ctx(self, bytes: &mut [u8], ctx: scroll::Endian) { |
| 115 |
/// use scroll::Cwrite; |
| 116 |
/// bytes.cwrite_with(self.foo, 0, ctx); |
| 117 |
/// bytes.cwrite_with(self.bar, 4, ctx); |
| 118 |
/// } |
| 119 |
/// } |
| 120 |
/// |
| 121 |
/// let bar = Bar { foo: -1, bar: 0xdeadbeef }; |
| 122 |
/// let mut bytes = [0x0; 16]; |
| 123 |
/// bytes.cwrite::<Bar>(bar, 0); |
| 124 |
/// ``` |
| 125 |
pub trait Cwrite<Ctx: Copy, I = usize>: Index<I> + IndexMut<RangeFrom<I>> { |
| 126 |
/// Writes `n` into `Self` at `offset`; uses default context. |
| 127 |
/// For the primitive types, this will be the **target machine**'s endianness. |
| 128 |
/// |
| 129 |
/// # Example |
| 130 |
/// |
| 131 |
/// ``` |
| 132 |
/// use scroll::{Cwrite, Cread}; |
| 133 |
/// let mut bytes = [0x0; 16]; |
| 134 |
/// bytes.cwrite::<i64>(42, 0); |
| 135 |
/// bytes.cwrite::<u32>(0xdeadbeef, 8); |
| 136 |
/// |
| 137 |
/// assert_eq!(bytes.cread::<i64>(0), 42); |
| 138 |
/// assert_eq!(bytes.cread::<u32>(8), 0xdeadbeef); |
| 139 |
#[inline] |
| 140 |
fn cwrite<N: IntoCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>(&mut self, n: N, offset: I) |
| 141 |
where |
| 142 |
Ctx: Default, |
| 143 |
{ |
| 144 |
let ctx = Ctx::default(); |
| 145 |
n.into_ctx(self.index_mut(offset..), ctx) |
| 146 |
} |
| 147 |
/// Writes `n` into `Self` at `offset` with `ctx` |
| 148 |
/// |
| 149 |
/// # Example |
| 150 |
/// |
| 151 |
/// ``` |
| 152 |
/// use scroll::{Cwrite, Cread, LE, BE}; |
| 153 |
/// let mut bytes = [0x0; 0x10]; |
| 154 |
/// bytes.cwrite_with::<i64>(42, 0, LE); |
| 155 |
/// bytes.cwrite_with::<u32>(0xdeadbeef, 8, BE); |
| 156 |
/// assert_eq!(bytes.cread_with::<i64>(0, LE), 42); |
| 157 |
/// assert_eq!(bytes.cread_with::<u32>(8, LE), 0xefbeadde); |
| 158 |
#[inline] |
| 159 |
fn cwrite_with<N: IntoCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>( |
| 160 |
&mut self, |
| 161 |
n: N, |
| 162 |
offset: I, |
| 163 |
ctx: Ctx, |
| 164 |
) { |
| 165 |
n.into_ctx(self.index_mut(offset..), ctx) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
impl<Ctx: Copy, I, W: ?Sized + Index<I> + IndexMut<RangeFrom<I>>> Cwrite<Ctx, I> for W {} |