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 TryFromCtx impl for &[u8] with no ctx. #102

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 23 additions & 15 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,29 @@ impl<'a> TryIntoCtx for &'a [u8] {
}
}

impl<'a> TryFromCtx<'a, usize> for &'a [u8] {
type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a [u8], size: usize) -> result::Result<(Self, usize), Self::Error> {
if size > src.len() {
Err(error::Error::TooBig {
size,
len: src.len(),
})
} else {
Ok((&src[..size], size))
}
}
}

impl<'a> TryFromCtx<'a> for &'a [u8] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the problem with having two contexts is that now e.g., this function call becomes ambiguous:

    const buf: &[u8] = &[0x1, 0x2, 0x3, 0x4];
    let res = buf.pread::<&[u8]>(0);

You should see an error like this after a bunch of generic vomit:

error[E0283]: type annotations needed
   --> tests/api.rs:388:19
    |
388 |     let res = buf.pread::<&[u8]>(0);
    |                   ^^^^^
    |
    = note: multiple `impl`s satisfying `&[u8]: TryFromCtx<'_, _>` found in the `scroll` crate:
            - impl<'a> TryFromCtx<'a, usize> for &'a [u8];
            - impl<'a> TryFromCtx<'a> for &'a [u8];

I'm very surprised we do not have a test in either api.rs or somewhere in lib.rs to ensure that existing preads of e.g., &[u8] are compilable. I believe introducing this could theoretically break downstream users as any code that did a pread of &[u8] will no longer compile.

I swear we had tests specifically to test that just pread worked fine on &[u8] but don't see any tests failing...

type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, usize), Self::Error> {
Ok((src, src.len()))
}
}

// TODO: make TryIntoCtx use StrCtx for awesomeness
impl<'a> TryIntoCtx for &'a str {
type Error = error::Error;
Expand Down Expand Up @@ -772,21 +795,6 @@ sizeof_impl!(i128);
sizeof_impl!(f32);
sizeof_impl!(f64);

impl<'a> TryFromCtx<'a, usize> for &'a [u8] {
type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a [u8], size: usize) -> result::Result<(Self, usize), Self::Error> {
if size > src.len() {
Err(error::Error::TooBig {
size,
len: src.len(),
})
} else {
Ok((&src[..size], size))
}
}
}

impl<'a, Ctx: Copy, T: TryFromCtx<'a, Ctx, Error = error::Error>, const N: usize>
TryFromCtx<'a, Ctx> for [T; N]
{
Expand Down
Loading