Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SizeWith impl for fixed array. #100

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading