Skip to content

Commit

Permalink
Document ZeroCopy derive
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Oct 10, 2023
1 parent e56fc70 commit d968908
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/musli-macros/src/zero_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn expand(cx: &Ctxt, input: &DeriveInput) -> Result<TokenStream, ()> {
let error: syn::Path = syn::parse_quote!(#krate::Error);
let validator: syn::Path = syn::parse_quote!(#krate::Validator);
let zero_copy: syn::Path = syn::parse_quote!(#krate::ZeroCopy);
let result: syn::Path = syn::parse_quote!(core::result::Result);
let result: syn::Path = syn::parse_quote!(#krate::__private::result::Result);

let mut const_asserts = Vec::new();
let mut padding = Vec::new();
Expand Down
72 changes: 72 additions & 0 deletions crates/musli-zerocopy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,73 @@ pub use self::r#unsized::Unsized;
mod r#unsized;

pub use self::zero_copy::{UnsizedZeroCopy, ZeroCopy};

/// Derive macro to implement [`ZeroCopy`].
///
/// # Examples
///
/// ```
/// use musli_zerocopy::{AlignedBuf, ZeroCopy};
///
/// #[derive(ZeroCopy)]
/// #[repr(C, align(128))]
/// struct Custom {
/// field: u32,
/// }
///
/// let mut buf = AlignedBuf::new();
/// buf.write(&Custom { field: 10 });
/// ```
///
/// # Attributes
///
/// ## Type attributes
///
/// The following `repr` attributes are supported:
/// * repr(C) - Ensures that the type has the mandatory represention.
/// * repr(transparent) - If there is a single field inside of the marked struct
/// which implements `ZeroCopy`.
/// * repr(align(..)) - Allows for control over the struct alignment.
///
/// The following `zero_copy(..)` attribute are supported:
///
/// ### `zero_copy(bounds = {<bound>,*})`
///
/// Allows for adding additional bounds to implement `ZeroCopy` for generic
/// types:
///
/// ```
/// use musli_zerocopy::ZeroCopy;
///
/// #[derive(ZeroCopy)]
/// #[repr(C)]
/// #[zero_copy(bounds = {A: ZeroCopy, B: ZeroCopy})]
/// struct Pair<A, B> {
/// left: A,
/// right: B,
/// }
/// ```
///
/// ### `zero_copy(crate = <path>)`
///
/// Allows for specifying a custom path to the `musli_zerocopy`` crate
/// (default).
///
/// ```
/// use musli_zerocopy as zerocopy;
///
/// use zerocopy::ZeroCopy;
///
/// #[derive(ZeroCopy)]
/// #[repr(C)]
/// #[zero_copy(crate = zerocopy)]
/// struct Custom {
/// field: u32,
/// }
/// ```
#[doc(inline)]
pub use musli_macros::ZeroCopy;

mod zero_copy;

mod map;
Expand All @@ -105,3 +172,8 @@ mod tests;
// Sentinel used to allow for `#[zero_copy(ignore)]` to work in this crate.
#[allow(unused)]
const ZERO_COPY_IGNORE_IS_NOT_SAFE_TO_USE: () = ();

#[doc(hidden)]
pub mod __private {
pub use ::core::result;
}
2 changes: 1 addition & 1 deletion crates/musli-zerocopy/src/pair.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::zero_copy::ZeroCopy;
use crate::ZeroCopy;

/// A pair of values which can be stored inside of any other zero copy
/// structure.
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-zerocopy/src/ptr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt;

use crate::zero_copy::ZeroCopy;
use crate::ZeroCopy;

/// An absolute pointer to a location in a [`Buf`].
///
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-zerocopy/src/ref.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::marker::PhantomData;

use crate::ptr::Ptr;
use crate::zero_copy::ZeroCopy;
use crate::ZeroCopy;

/// A sized reference.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-zerocopy/src/slice.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::marker::PhantomData;

use crate::ptr::Ptr;
use crate::zero_copy::ZeroCopy;
use crate::ZeroCopy;

/// A reference to a slice packed as a wide pointer.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-zerocopy/src/unsized.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::marker::PhantomData;

use crate::ptr::Ptr;
use crate::zero_copy::ZeroCopy;
use crate::ZeroCopy;

/// A reference to an unsized value packed as a wide pointer.
///
Expand Down
3 changes: 0 additions & 3 deletions crates/musli-zerocopy/src/zero_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ use core::str;
use crate::buf::{AnyValue, Buf, BufMut};
use crate::error::{Error, ErrorKind};

#[doc(inline)]
pub use musli_macros::ZeroCopy;

/// Trait governing which `T` in [`Unsized<T>`] the wrapper can handle.
///
/// We only support slice-like, unaligned unsized types, such as `str` and
Expand Down

0 comments on commit d968908

Please sign in to comment.