Announcing zerocopy 0.8! #1680
Pinned
joshlf
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We are pleased to announce the release of zerocopy 0.8 (crate documentation). 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 Dataful Enums
Zerocopy now has support for dataful enums; e.g.:
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.
Upgrading guide
Our new 0.8 release includes a number of changes which are backwards-incompatible with 0.7. This guide describes these changes and gives advice for how to make the upgrade process as painless as possible.
For large codebases, it may help to automate some of these upgrading steps using a tool such as ast-grep.
Implied derives
The
FromBytes
trait is a sub-trait ofFromZeros
. On 0.7, in order to deriveFromBytes
, you also had to deriveFromZeros
explicitly:On 0.8, deriving a trait automatically derives its super-traits. This code will now fail to compile since the
FromBytes
derive will emit an impl ofFromZeros
, which will conflict with the impl derived by the explicitFromZeros
derive. Instead, simply write:New names
The
AsBytes
trait has been renamed toIntoBytes
. TheFromZeroes
trait has been renamed toFromZeros
.A number of
Ref
,FromBytes
, andIntoBytes
methods have been renamed or deprecated in favor of a more general method. The old methods are preserved and marked as#[doc(hidden)] #[deprecated]
, including a deprecation message that indicates which API should be preferred in 0.8. We recommend using these compilation warnings as a guide for how to upgrade individual call sites. As they are#[doc(hidden)]
, we do not consider these methods subject to semver guarantees, and they may be removed or re-used in any future 0.8 version.New traits
FromBytes
andIntoBytes
Some properties that were previously required by
FromBytes
orIntoBytes
(previouslyAsBytes
) have now been split into separate traits. APIs that still require those properties now add bounds on these new traits.The new traits in question are
KnownLayout
, which encodes certain layout information about a type, andImmutable
, which indicates that a type does not contain anyUnsafeCell
s.We recommend that you simply derive
KnownLayout
andImmutable
on any type with existing zerocopy trait derives. Alternatively, for a more fine-grained approach, use compilation errors as a guide to which types should derive these traits.ByteSlice
The
ByteSlice
trait now supports byte slices which cannot be cheaply split in two. The newSplitByteSlice
trait extendsByteSlice
with this behavior. In 0.8,SplitByteSlice
is analogous to whatByteSlice
was in 0.7. The same holds forByteSliceMut
andSplitByteSliceMut
.If you don't care about maximum flexibility, we recommend simply replacing all instances of
ByteSlice[Mut]
withSplitByteSlice[Mut]
. Alternatively, if you wish to supportByteSlice[Mut]
types which cannot be split, use compilation errors as a guide to where you need to addSplitByteSlice[Mut]
bounds.New return values
Many methods that previously returned
Option
now returnResult
, and include a finer-grained error type. If you wish to preserve the 0.7 behavior and minimize the code change needed to upgrade, simply add.ok()
to these call sites in order to convertResult
s back intoOption
s. However, we recommend that most code should try to migrate to the new error types eventually, as they will help you ensure that you're only handling errors that you expect. For example, having to handle anAlignmentError
where you don't expect it is an indication that you could use a different API that guarantees freedom from alignment errors (e.g. one of theRef
methods withunaligned
in the method name).IntoBytes
support for unionsPutting
#[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.byteorder
featureThe
byteorder
feature has been removed; in 0.8, thebyteorder
module is always present, and is not optional.Beta Was this translation helpful? Give feedback.
All reactions