-
Notifications
You must be signed in to change notification settings - Fork 142
Open
Labels
compatibility-nonbreakingChanges that are (likely to be) non-breakingChanges that are (likely to be) non-breaking
Description
See also: #2273
Especially in combination with the FromZeroes trait, it would be useful to be able to represent statically that a sequence of bytes are currently zero. Some operations which produce guaranteed-zero bytes (such as allocating new virtual memory pages) could get a performance or safety benefit from this functionality.
One hypothetical API would look like this, but there could be other ways of designing this:
// All bytes are 0. Note that there may be subtle interactions with
// interior mutability when handing out immutable references.
#[repr(transparent)]
pub struct Zero<T: ?Sized>(T);
// `T: AsBytes` allows us to inspect `T`'s bytes to confirm that they're all 0
impl<T: AsBytes> Zero<T> {
pub fn new(t: T) -> Option<Zero<T>> { ... }
pub fn try_from_slice(ts: &[T]) -> Option<&[Zero<T>]> { ... }
}
impl<T> Zero<T> {
pub unsafe fn new_unchecked(t: T) -> Zero<T> { ... }
}
impl<T: ?Sized + AsRef> Zero<T> {
pub fn try_from_ref(t: &T) -> Option<&Zero<T>> { ... }
}
impl<T: ?Sized> Zero<T> {
pub unsafe fn from_ref_unchecked(t: &T) -> &Zero<T> { ... }
}
impl<T: ?Sized> Deref for Zero<T> { ... }
pub unsafe trait FromZeroes {
fn from_bytes(bytes: Zero<ByteSlice<Self>>) -> Self { ... }
fn from_slice(bytes: &[Zero<u8>]) -> Option<&[Self]> { ... }
// Maybe modify existing zeroing methods to return a `Zero`?
fn new_zeroed() -> Zero<Self> { ... }
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
compatibility-nonbreakingChanges that are (likely to be) non-breakingChanges that are (likely to be) non-breaking