Announcing zerocopy 0.8! #1680
Closed
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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)UnsafeCells...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
KnownLayouttrait, which abstracts over the distinction betweenSizedtypes, 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
TryFromBytestrait. 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 implementingTryFromBytescan be used as the destination type for invocations our new macros,try_transmute,try_transmute_refandtry_transmute_mut. In our upcoming releases, we plan to add support for custom validators.Support for
UnsafeCells, AtomicsZerocopy's by-value and by-mut conversions now support types containing
UnsafeCells. This enables conversions involving types that permit interior mutability, like atomics; e.g.:By-value and by-mut transmutations support types containing
UnsafeCells. By-ref transmutations only permit types which do not containUnsafeCells; this is enforced using the newImmutabletrait.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->IntoBytesFromZeroes->FromZerosFromBytesmethodsRefmethodsSuccinct 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)], andTryFromBytesandFromZerosare derived automatically.More
const-friendly APIsOn the
byteordermodule's types, some methods are nowconst. Specifically,from_bytes,to_bytes,new, andget.Permit external impls of
ByteSliceThe
ByteSlicetrait is no longer sealed; it can be implemented by downstream crates. The same is true of sub-traits such asSplitByteSliceandIntoByteSlice.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
FromBytestrait is a sub-trait ofFromZeros. On 0.7, in order to deriveFromBytes, you also had to deriveFromZerosexplicitly:On 0.8, deriving a trait automatically derives its super-traits. This code will now fail to compile since the
FromBytesderive will emit an impl ofFromZeros, which will conflict with the impl derived by the explicitFromZerosderive. Instead, simply write:New names
The
AsBytestrait has been renamed toIntoBytes. TheFromZeroestrait has been renamed toFromZeros.A number of
Ref,FromBytes, andIntoBytesmethods 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
FromBytesandIntoBytesSome properties that were previously required by
FromBytesorIntoBytes(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 anyUnsafeCells.We recommend that you simply derive
KnownLayoutandImmutableon 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.ByteSliceThe
ByteSlicetrait now supports byte slices which cannot be cheaply split in two. The newSplitByteSlicetrait extendsByteSlicewith this behavior. In 0.8,SplitByteSliceis analogous to whatByteSlicewas in 0.7. The same holds forByteSliceMutandSplitByteSliceMut.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
Optionnow 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 convertResults back intoOptions. 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 anAlignmentErrorwhere 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 theRefmethods withunalignedin the method name).IntoBytessupport for unionsPutting
#[derive(IntoBytes)]on a union now requires passing--cfg zerocopy_derive_union_into_bytesin 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 useIntoByteson unions, please let us know.byteorderfeatureThe
byteorderfeature has been removed; in 0.8, thebyteordermodule is always present, and is not optional.Beta Was this translation helpful? Give feedback.
All reactions