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
- Pick public TryFromBytes method names that are more consistent with
  FromBytes? In particular:
  - try_from_ref -> try_from_bytes
  - try_from_mut -> try_from_bytes_mut

Makes progress on #5
  • Loading branch information
joshlf committed Sep 19, 2023
1 parent cc7a0eb commit baa448f
Show file tree
Hide file tree
Showing 4 changed files with 1,010 additions and 173 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,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 baa448f

Please sign in to comment.