Feedback requested! 0.8 pre-release #1288
Replies: 4 comments 6 replies
-
A few notes from a preliminary pass at updating a fairly large project (crosvm) - I just did enough to make it compile and did not attempt to take advantage of any new features or do any major cleanups. Main takeaway: the new version works (our tests pass), woohoo! However, the upgrade required a lot of manual tweaks beyond the expected find-and-replace for the renamed types and functions. Pretty much every type that previously had It might be nice to have a super-trait that pulls in all of the other traits for "normal" structs ( The compiler errors when a type is missing
Taking the compiler's suggestion results in a different error with a recommendation to remove the reference (back to the original code). Error types like |
Beta Was this translation helpful? Give feedback.
-
@jswrenn Maybe we should mention the new transmute macros we've added? |
Beta Was this translation helpful? Give feedback.
-
It would be useful to include a TryFromBytes example with a custom 'string' type. I deal with a lot of binary file types that store a string as a u16/u32 length + [u8]/str data, which seems like a perfect use of Dynamically Sized Types. The only problem is that KnownLayout doesn't implement str like it does [u8], so I'm unsure of the best way to go about making that type. It looks like that's still a TODO on #29 / #671? I would also like to see #158 resolved, but I understand if you can't fit that in for 0.8. I looked and it's since been refactored and inlined google/crosvm@742791d, but would look something like this in 0.8: pub fn zerocopy_from_reader<R: io::Read, T: FromBytes + IntoBytes + FromZeros>(
mut read: R,
) -> io::Result<T> {
let mut out = T::new_zeroed();
read.read_exact(out.as_mut_bytes())?;
Ok(out)
} |
Beta Was this translation helpful? Give feedback.
-
It would be helpful to have a
@jswrenn on Discord:
|
Beta Was this translation helpful? Give feedback.
-
EDIT: We've released 0.8!
This is a draft of our 0.8 release notes. We would love to get your feedback! Please kick the tires on our latest 0.8 alpha release and let us know what's working for you, what's not, what we should change, etc. Any and all feedback is welcome!
These notes accurately describe the state of our current 0.8 alpha releases, but small API changes may still happen before 0.8 is stabilized. If any such changes happen, they will likely just affect method naming.
We are pleased to announce the release of zerocopy 0.8. Zerocopy is a crate that provides safe abstractions for transmutation, and it is the foundation of a wide variety of security-critical systems software — networking stacks, filesystems, cryptograhic firmware, and more — at AWS, Google, and others. In this release, we have extended our safe transmute analysis to support a broader range of types, including:
bool
)UnsafeCell
s...among other improvements.
Call for Support
Success stories like the above both delight us and are crucial to demonstrating to our employeers that zerocopy is a worthy investment of engineering time. If our work has made your life easier, please let us know! Zerocopy's continued development depends on your suppport.
Support for Dynamically Sized Types
Zerocopy now has comprehensive support for slice-based dynamically-sized types (slice DSTs). A slice DST has a fixed prefix followed by a variable number of trailing elements. Slice DSTs are especially useful when modeling packet or file formats with variable-length fields; e.g.:
Zerocopy supports these types via the new
KnownLayout
trait, which abstracts over the distinction betweenSized
types, slice types, and slice DSTs.Support for Fallible Conversions
Zerocopy now supports conditionally correct transmutations — performing only the minimum set of runtime checks to confirm that the source value is a valid instance of the destination type. For example:
This functionality is made possible by our new, derivable
TryFromBytes
trait. You can use this trait to model formats with magic numbers; e.g.:At the time of release,
derive(TryFromBytes)
works on structs, unions, and enums. Types implementingTryFromBytes
can be used as the destination type for invocations our new macros,try_transmute
,try_transmute_ref
andtry_transmute_mut
. In our upcoming releases, we plan to add support for custom validators.Support for
UnsafeCell
s, AtomicsZerocopy's by-value and by-mut conversions now support types containing
UnsafeCell
s. This enables conversions involving types that permit interior mutability, like atomics; e.g.:By-value and by-mut transmutations support types containing
UnsafeCell
s. By-ref transmutations only permit types which do not containUnsafeCell
s; this is enforced using the newImmutable
trait.Other Improvements
Return Type Overhaul
Zerocopy's conversion APIs now consistently return
Result
, with the error type providing both the logical cause of the failure and the source value that precipated the failure. The error type is fine-grained, allowing the user to reason about what error conditions are possible.This directly supports the common usage pattern of incrementally parsing from a buffer, makes certain error-handling patterns possible, and drastically improves error messages; e.g.:
Different APIs have different error conditions; these are now accurately modeled by those APIs' error types:
For types with no alignment requirement, alignment errors are impossible. In these cases, our error types support discarding any alignment error infallibly:
API Cleanup
Some traits and methods have been renamed so that our API naming is more consistent. In all cases, the old name remains either as a
#[doc(hidden)]
/#[deprecated]
method or asuse NewName as OldName
(notpub use
; uses of the old name are banned, but generate a helpful compiler error). Notable renames include:AsBytes
->IntoBytes
FromZeroes
->FromZeros
FromBytes
methodsRef
methodsSuccinct Derives
Previously, our derives required that all derived traits be explicitly named - e.g.,
#[derive(TryFromBytes, FromZeros, FromBytes)]
. As of 0.8, deriving a trait automatically derives any super-traits - e.g., you can now just write#[derive(FromBytes)]
, andTryFromBytes
andFromZeros
are derived automatically.More
const
-friendly APIsOn the
byteorder
module's types, some methods are nowconst
. Specifically,from_bytes
,to_bytes
,new
, andget
.Permit external impls of
ByteSlice
The
ByteSlice
trait is no longer sealed; it can be implemented by downstream crates. The same is true of sub-traits such asSplitByteSlice
andIntoByteSlice
.Lower MSRV to 1.56
We've reduced our MSRV from 1.60 to 1.56.
Miscellaneous
Putting
#[derive(IntoBytes)]
on a union now requires passing--cfg zerocopy_derive_union_into_bytes
in order to gate against potential future unsoundness. We're actively working with Rust to stabilize the necessary language guarantees to support this in a forwards-compatible way. As part of this effort, we need to know how much demand there is for this feature. If you would like to useIntoBytes
on unions, please let us know.Beta Was this translation helpful? Give feedback.
All reactions