Skip to content

Commit 5024e56

Browse files
authored
Improve transmute*! documentation (#661)
This PR adds examples to the documentation of `transmute!`, `transmute_ref!` and `transmute_mut!`. Makes progress towards #32.
1 parent b20a583 commit 5024e56

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/lib.rs

+37
Original file line numberDiff line numberDiff line change
@@ -3400,6 +3400,17 @@ mod simd {
34003400
/// Note that the `T` produced by the expression `$e` will *not* be dropped.
34013401
/// Semantically, its bits will be copied into a new value of type `U`, the
34023402
/// original `T` will be forgotten, and the value of type `U` will be returned.
3403+
///
3404+
/// # Examples
3405+
///
3406+
/// ```
3407+
/// # use zerocopy::transmute;
3408+
/// let one_dimensional: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
3409+
///
3410+
/// let two_dimensional: [[u8; 4]; 2] = transmute!(one_dimensional);
3411+
///
3412+
/// assert_eq!(two_dimensional, [[0, 1, 2, 3], [4, 5, 6, 7]]);
3413+
/// ```
34033414
#[macro_export]
34043415
macro_rules! transmute {
34053416
($e:expr) => {{
@@ -3455,6 +3466,17 @@ macro_rules! transmute {
34553466
/// The lifetime of the input type, `&T` or `&mut T`, must be the same as or
34563467
/// outlive the lifetime of the output type, `&U`.
34573468
///
3469+
/// # Examples
3470+
///
3471+
/// ```
3472+
/// # use zerocopy::transmute_ref;
3473+
/// let one_dimensional: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
3474+
///
3475+
/// let two_dimensional: &[[u8; 4]; 2] = transmute_ref!(&one_dimensional);
3476+
///
3477+
/// assert_eq!(two_dimensional, &[[0, 1, 2, 3], [4, 5, 6, 7]]);
3478+
/// ```
3479+
///
34583480
/// # Alignment increase error message
34593481
///
34603482
/// Because of limitations on macros, the error message generated when
@@ -3550,6 +3572,21 @@ macro_rules! transmute_ref {
35503572
/// The lifetime of the input type, `&mut T`, must be the same as or outlive the
35513573
/// lifetime of the output type, `&mut U`.
35523574
///
3575+
/// # Examples
3576+
///
3577+
/// ```
3578+
/// # use zerocopy::transmute_mut;
3579+
/// let mut one_dimensional: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
3580+
///
3581+
/// let two_dimensional: &mut [[u8; 4]; 2] = transmute_mut!(&mut one_dimensional);
3582+
///
3583+
/// assert_eq!(two_dimensional, &[[0, 1, 2, 3], [4, 5, 6, 7]]);
3584+
///
3585+
/// two_dimensional.reverse();
3586+
///
3587+
/// assert_eq!(one_dimensional, [4, 5, 6, 7, 0, 1, 2, 3]);
3588+
/// ```
3589+
///
35533590
/// # Alignment increase error message
35543591
///
35553592
/// Because of limitations on macros, the error message generated when

0 commit comments

Comments
 (0)