-
Notifications
You must be signed in to change notification settings - Fork 124
Open
Description
See also: #5 (comment)
This is an extension of the design described in #5
TODO: Fill out details
pub unsafe trait TryFromBytes {
// This can either default to `()` by dint of the custom derive emitting that,
// or we can use associated type defaults once they're stable.
#[doc(hidden)]
type Context: Default;
#[doc(hidden)]
fn is_bit_valid(ctx: &mut Self::Context, ptr: NonNull<Self>) -> bool;
fn try_read_from(bytes: &[u8]) -> Option<Self> where Self: Sized {
let maybe_uninit = MaybeUninit::<Self>::read_from(bytes)?;
let ptr = NonNull::from(&maybe_uninit).cast::<Self>();
let mut ctx = <Self::Context as Default>::default();
if Self::is_bit_valid(&mut ctx, ptr) {
unsafe { maybe_uninit.assume_init() }
}
}
}
Per @djkoloski, for rkyv's use case, the Context
type might actually need to be constructible from the byte slice that's being parsed from, ie type Context: for<'a> From<&'a [u8]>
.
Metadata
Metadata
Assignees
Labels
No labels