Skip to content

Commit

Permalink
ctx: Added SizeWith impl for fixed array. (#100)
Browse files Browse the repository at this point in the history
* Added SizeWith impl for fixed array.
  • Loading branch information
Frostie314159 authored Aug 12, 2024
1 parent eb3bc08 commit de64a94
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,11 @@ impl<Ctx: Copy, T: TryIntoCtx<Ctx, Error = error::Error>, const N: usize> TryInt
Ok(offset)
}
}
impl<Ctx, T: SizeWith<Ctx>, const N: usize> SizeWith<Ctx> for [T; N] {
fn size_with(ctx: &Ctx) -> usize {
T::size_with(ctx) * N
}
}

#[cfg(feature = "std")]
impl<'a> TryFromCtx<'a> for &'a CStr {
Expand Down
7 changes: 6 additions & 1 deletion tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::ops::{Deref, DerefMut};

use scroll::ctx::SizeWith as _;
use scroll::{ctx, Cread, Pread, Result};
use scroll::{ctx, Cread, Endian, Pread, Result};

#[derive(Default)]
pub struct Section<'a> {
Expand Down Expand Up @@ -376,3 +376,8 @@ fn test_fixed_array_rw() {
buf.pwrite([0x1337u16, 0x1337], 0).unwrap();
assert_eq!(buf, bytes);
}

#[test]
fn test_fixed_array_size_with() {
assert_eq!(<[u32; 3]>::size_with(&Endian::Little), 12);
}

0 comments on commit de64a94

Please sign in to comment.