-
Notifications
You must be signed in to change notification settings - Fork 37
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In principle I agree this is extremely useful; however, and perhaps I'm misremembering, but i recall many years ago, when i tried to add multiple ctx's for a single type, the compiler at client invocation site, was not able to properly infer which context to use, for e.g., pread
(not pread_with
); i decided this was not an acceptable ergonomic decision for users (myself primarily at the time) so I did not add this.
Maybe there is a solution around this (without changing all of scroll), but I don't know. I realize this may be disappointing, but I hope you can see why the ambiguous compile error is problematic/not desirable, from both backwards compat standpoints as well as ergonomics?
} | ||
} | ||
|
||
impl<'a> TryFromCtx<'a> for &'a [u8] { |
There was a problem hiding this comment.
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...
I'd also like to learn more about your usecase, could perhaps post a snippet of the type of also thanks for all your work on this crate so far and following up on issues ! :) |
My usecase for this is, that in my library I have a type
|
Getting back to this now, so:
Maybe I'm misunderstanding you, but pread doesn't use a "default" ctx, insofar as it only requires the That being said, none of this actually matters w.r.t. the ambiguous trait dispatch. The heart of the issue is that when someone implements > 1 This was a conscious decision to not burden the user with this, or to have the Ctx appear in the generic bound of a The original design goals of scroll were roughly, ergonomic user api (not really ergonomic for library author(s) 😆 ), very generic (see how far could take it), offset based, allow user defined types, support deriving. I don't think it's in the cards that scroll will ever change in that regard (somewhat ergonomic user api), but if you have some idea theoretically how the existing design could potentially be redesigned (while keeping most/all of the ergonomics of current design) to allow multiple ====================== I'd like to understand your usecase a bit more/solve it though; is it possible for you to paste maybe a simple repro with what you'd like to do/expect? Maybe something like: let data = bytes.pread::<Foo<Bar>>(0)?;
let data2 = bytes.pread::<Foo<&[u8]>>()?; // does not work today? I'm also curious if you tested out this patch on your specific usecase, and how you did not hit the ambiguous trait dispatch error i posted above? Anyway, this was a long comment, thanks for reading this far, and hope you're having a good day/night :) |
Hi, this PR adds an implementation of
TryFromCtx
for&[u8]
, with no context. Instead, the whole input slice is converted. This is useful, if a struct has a variable length body and users may further specify that type through a generic.