Skip to content

Commit

Permalink
Include try_transmute! in crate-level overview (#1267)
Browse files Browse the repository at this point in the history
  • Loading branch information
jswrenn authored May 16, 2024
1 parent 7e107d8 commit 6d52f4f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ You should generally derive these marker traits whenever possible.

###### Conversion Macros

Zerocopy provides three macros for safe, zero-cost casting between types:
Zerocopy provides four macros for safe, zero-cost casting between types:

- `transmute` converts a value of one type to a value of another type of
the same size
- (`try_`[try_transmute])`transmute` (conditionally) converts a value of
one type to a value of another type of the same size
- `transmute_mut` converts a mutable reference of one type to a mutable
reference of another type of the same size
- `transmute_ref` converts a mutable or immutable reference
Expand Down
20 changes: 13 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
//!
//! ##### Conversion Macros
//!
//! Zerocopy provides three macros for safe, zero-cost casting between types:
//! Zerocopy provides four macros for safe, zero-cost casting between types:
//!
//! - [`transmute`] converts a value of one type to a value of another type of
//! the same size
//! - ([`try_`][try_transmute])[`transmute`] (conditionally) converts a value of
//! one type to a value of another type of the same size
//! - [`transmute_mut`] converts a mutable reference of one type to a mutable
//! reference of another type of the same size
//! - [`transmute_ref`] converts a mutable or immutable reference
Expand Down Expand Up @@ -4440,12 +4440,18 @@ macro_rules! transmute_mut {
/// # Examples
///
/// ```
/// # use zerocopy::try_transmute;
/// # use zerocopy::*;
/// // 0u8 → bool = false
/// assert_eq!(try_transmute!(0u8), Ok(false));
/// assert_eq!(try_transmute!(1u8), Ok(true));
///
/// let maybe_bool: Result<bool, _> = try_transmute!(255u8);
/// assert_eq!(maybe_bool.unwrap_err().into_src(), 255u8);
/// // 1u8 → bool = true
/// assert_eq!(try_transmute!(1u8), Ok(true));
///
/// // 2u8 → bool = error
/// assert!(matches!(
/// try_transmute!(3u8),
/// Result::<bool, _>::Err(ValidityError { .. })
/// ));
///
/// ```
#[macro_export]
Expand Down

0 comments on commit 6d52f4f

Please sign in to comment.