Skip to content

Commit

Permalink
Document #[key(bitset)]
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Apr 26, 2023
1 parent 77fbad7 commit ac03973
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
41 changes: 37 additions & 4 deletions fixed-map-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,45 @@ mod unit_variants;

/// Derive to implement the `Key` trait.
///
/// Requires that `fixed_map` is in scope.
///
/// This derive implements the `Key` trait for a given type.
///
/// The `Key` trait is what allows `fixed_map` to set up storage for a type that will be the key in
/// a fixed map.
/// The `Key` trait is what allows `fixed_map` to set up storage for a type that
/// will be the key in a fixed map.
///
/// # Container attributes
///
/// #### `#[key(bitset)]`
///
/// This ensures that backing storage is performed with a bitset when used with
/// a [`Set`].
///
/// ```rust
/// use fixed_map::{Key, Set};
///
/// #[derive(Clone, Copy, Key)]
/// pub enum Regular {
/// First,
/// Second,
/// Third,
/// }
///
///
/// #[derive(Clone, Copy, Key)]
/// #[key(bitset)]
/// pub enum Bits {
/// First,
/// Second,
/// Third,
/// }
///
/// // Normal storage uses an array of booleans:
/// assert_eq!(std::mem::size_of::<Set<Regular>>(), 3);
///
/// // Bitset storage uses a single u8 (or other appropriate type based on size):
/// assert_eq!(std::mem::size_of::<Set<Bits>>(), 1);
/// ```
///
/// # Guide
///
/// Given the following enum:
///
Expand Down
7 changes: 1 addition & 6 deletions fixed-map-derive/src/unit_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, opts: &Opts, en: &DataEnum) -> Result<Tok
let entry_impl = impl_entry(cx, &map_storage)?;
let map_storage_impl = impl_map(cx, en, &map_storage, &names)?;

let set_storage_impl = if let Some(span) = opts.bitset {
if !cfg!(fixed_map_experimental) {
cx.span_error(span, "trying to use experimental feature `bitset` without specifying `--cfg fixed_map_experimental`");
return Err(());
}

let set_storage_impl = if opts.bitset.is_some() {
impl_bitset(cx, en, &set_storage)?
} else {
impl_set(cx, en, &set_storage, &names)?
Expand Down

0 comments on commit ac03973

Please sign in to comment.