Skip to content

Commit

Permalink
Add TryFromBytes trait
Browse files Browse the repository at this point in the history
`TryFromBytes` can be implemented for types which are not `FromZeroes`
or `FromBytes`; it supports performing a runtime check to determine
whether a given byte sequence contains a valid instance of `Self`.

This is the first step of #5. Future commits will add support for a
custom derive and for implementing `TryFromBytes` on unsized types.

TODO:
- More thorough tests for non-FromBytes types (bool, char, etc)
- Tests that go through the `TryFromBytes` public methods rather than
  testing `is_bit_valid` directly
- Update safety requirements for `is_bit_valid` now that it takes a
  `Ptr` rather than a `NonNull`
- Update SAFETY comments in various places

Makes progress on #5
  • Loading branch information
joshlf authored and jswrenn committed Nov 9, 2023
1 parent 40acb81 commit 806a8d7
Show file tree
Hide file tree
Showing 4 changed files with 1,055 additions and 176 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ so you don't have to.

## Overview

Zerocopy provides four core marker traits, each of which can be derived
Zerocopy provides five core marker traits, each of which can be derived
(e.g., `#[derive(FromZeroes)]`):
- `FromZeroes` indicates that a sequence of zero bytes represents a valid
instance of a type
- `FromBytes` indicates that a type may safely be converted from an
arbitrary byte sequence
- `TryFromBytes` supports non-`FromBytes` types by providing the ability
to check the validity of a conversion at runtime
- `AsBytes` indicates that a type may safely be converted *to* a byte
sequence
- `Unaligned` indicates that a type's alignment requirement is 1
Expand Down
Loading

0 comments on commit 806a8d7

Please sign in to comment.