Skip to content

Commit 1fb0880

Browse files
committed
Improve documentation
1 parent ac03973 commit 1fb0880

File tree

4 files changed

+175
-155
lines changed

4 files changed

+175
-155
lines changed

fixed-map-derive/src/lib.rs

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -68,111 +68,6 @@ mod context;
6868
mod symbol;
6969
mod unit_variants;
7070

71-
/// Derive to implement the `Key` trait.
72-
///
73-
/// This derive implements the `Key` trait for a given type.
74-
///
75-
/// The `Key` trait is what allows `fixed_map` to set up storage for a type that
76-
/// will be the key in a fixed map.
77-
///
78-
/// # Container attributes
79-
///
80-
/// #### `#[key(bitset)]`
81-
///
82-
/// This ensures that backing storage is performed with a bitset when used with
83-
/// a [`Set`].
84-
///
85-
/// ```rust
86-
/// use fixed_map::{Key, Set};
87-
///
88-
/// #[derive(Clone, Copy, Key)]
89-
/// pub enum Regular {
90-
/// First,
91-
/// Second,
92-
/// Third,
93-
/// }
94-
///
95-
///
96-
/// #[derive(Clone, Copy, Key)]
97-
/// #[key(bitset)]
98-
/// pub enum Bits {
99-
/// First,
100-
/// Second,
101-
/// Third,
102-
/// }
103-
///
104-
/// // Normal storage uses an array of booleans:
105-
/// assert_eq!(std::mem::size_of::<Set<Regular>>(), 3);
106-
///
107-
/// // Bitset storage uses a single u8 (or other appropriate type based on size):
108-
/// assert_eq!(std::mem::size_of::<Set<Bits>>(), 1);
109-
/// ```
110-
///
111-
/// # Guide
112-
///
113-
/// Given the following enum:
114-
///
115-
/// ```rust
116-
/// use fixed_map::Key;
117-
///
118-
/// #[derive(Clone, Copy, Key)]
119-
/// pub enum MyKey {
120-
/// First,
121-
/// Second,
122-
/// Third,
123-
/// }
124-
/// ```
125-
///
126-
/// It performs the following simplified expansion:
127-
///
128-
/// ```rust,no_compile,no_run
129-
/// use fixed_map::Key;
130-
///
131-
/// #[derive(Clone, Copy)]
132-
/// pub enum MyKey {
133-
/// First,
134-
/// Second,
135-
/// Third,
136-
/// }
137-
///
138-
/// /// Build a storage struct containing an item for each key:
139-
/// pub struct MyKeyStorage<V> {
140-
/// /// Storage for `MyKey::First`.
141-
/// f1: Option<V>,
142-
/// /// Storage for `MyKey::Second`.
143-
/// f2: Option<V>,
144-
/// /// Storage for `MyKey::Third`.
145-
/// f3: Option<V>,
146-
/// }
147-
///
148-
/// /// Implement storage for `KeyStorage`.
149-
/// impl<V> fixed_map::storage::Storage<MyKey, V> for KeyStorage<V> {
150-
/// fn get(&self, key: MyKey) -> Option<&V> {
151-
/// match key {
152-
/// MyKey::First => self.f1.as_ref(),
153-
/// MyKey::Second => self.f2.as_ref(),
154-
/// MyKey::Third => self.f3.as_ref(),
155-
/// }
156-
/// }
157-
///
158-
/// /* skipped */
159-
/// }
160-
///
161-
/// impl<V> Default for KeyStorage<V> {
162-
/// fn default() -> Self {
163-
/// Self {
164-
/// f1: None,
165-
/// f2: None,
166-
/// f3: None,
167-
/// }
168-
/// }
169-
/// }
170-
///
171-
/// /// Implement the `Key` trait to point out storage.
172-
/// impl<V> fixed_map::Key<Key, V> for MyKey {
173-
/// type Storage = KeyStorage<V>;
174-
/// }
175-
/// ```
17671
#[proc_macro_derive(Key, attributes(key))]
17772
pub fn storage_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
17873
let ast = syn::parse_macro_input!(input as DeriveInput);
@@ -193,7 +88,6 @@ pub fn storage_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream
19388
quote!(#(#compile_errors)*).into()
19489
}
19590

196-
/// Derive to implement the `Key` trait.
19791
fn impl_storage(cx: &context::Ctxt<'_>) -> Result<TokenStream, ()> {
19892
let opts = attrs::parse(cx)?;
19993

src/key.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ use crate::set::storage::{BooleanSetStorage, OptionSetStorage, SetStorage, Singl
123123
/// [`Map`]: crate::Map
124124
/// [`Set`]: crate::Set
125125
pub trait Key: Copy {
126-
/// The [`Map`] storage implementation to use for the key implementing
127-
/// this trait.
126+
/// The [`Map`][crate::Map] storage implementation to use for the key
127+
/// implementing this trait.
128128
type MapStorage<V>: MapStorage<Self, V>;
129129

130-
/// The [`Set`] storage implementation to use for the key implementing
131-
/// this trait.
130+
/// The [`Set`][crate::Set] storage implementation to use for the key
131+
/// implementing this trait.
132132
type SetStorage: SetStorage<Self>;
133133
}
134134

src/lib.rs

Lines changed: 170 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -284,54 +284,180 @@
284284
#![no_std]
285285
#![deny(unsafe_code)]
286286
#![deny(missing_docs)]
287-
// Enable pedantic lints as warnings so we don't break builds when
288-
// lints are modified or new lints are added to clippy.
289-
#![warn(
290-
// Enable more useful rustc lints
291-
absolute_paths_not_starting_with_crate,
292-
elided_lifetimes_in_paths,
293-
explicit_outlives_requirements,
294-
keyword_idents,
295-
macro_use_extern_crate,
296-
meta_variable_misuse,
297-
missing_copy_implementations,
298-
non_ascii_idents,
299-
noop_method_call,
300-
pointer_structural_match,
301-
single_use_lifetimes,
302-
trivial_casts,
303-
trivial_numeric_casts,
304-
unreachable_pub,
305-
unused_extern_crates,
306-
unused_import_braces,
307-
unused_lifetimes,
308-
unused_macro_rules,
309-
unused_qualifications,
310-
unused_tuple_struct_fields,
311-
variant_size_differences,
312-
// Enable pedantic clippy lints
313-
clippy::pedantic,
314-
// Useful clippy lints for no_std support
315-
clippy::std_instead_of_core,
316-
clippy::std_instead_of_alloc,
317-
clippy::alloc_instead_of_core,
318-
// Useful extra perf lints
319-
clippy::missing_inline_in_public_items
320-
)]
321-
// `clippy::pedantic` exceptions
322-
#![allow(
323-
// style choice
324-
clippy::module_name_repetitions,
325-
// false positive
326-
clippy::type_repetition_in_bounds,
327-
// false positive
328-
clippy::expl_impl_clone_on_copy
329-
)]
287+
#![warn(absolute_paths_not_starting_with_crate)]
288+
#![warn(elided_lifetimes_in_paths)]
289+
#![warn(explicit_outlives_requirements)]
290+
#![warn(keyword_idents)]
291+
#![warn(macro_use_extern_crate)]
292+
#![warn(meta_variable_misuse)]
293+
#![warn(missing_copy_implementations)]
294+
#![warn(non_ascii_idents)]
295+
#![warn(noop_method_call)]
296+
#![warn(pointer_structural_match)]
297+
#![warn(single_use_lifetimes)]
298+
#![warn(trivial_casts)]
299+
#![warn(trivial_numeric_casts)]
300+
#![warn(unreachable_pub)]
301+
#![warn(unused_extern_crates)]
302+
#![warn(unused_import_braces)]
303+
#![warn(unused_lifetimes)]
304+
#![warn(unused_macro_rules)]
305+
#![warn(unused_qualifications)]
306+
#![warn(unused_tuple_struct_fields)]
307+
#![warn(variant_size_differences)]
308+
#![warn(clippy::pedantic)]
309+
#![warn(clippy::std_instead_of_core)]
310+
#![warn(clippy::std_instead_of_alloc)]
311+
#![warn(clippy::alloc_instead_of_core)]
312+
#![warn(clippy::missing_inline_in_public_items)]
313+
#![allow(clippy::module_name_repetitions)]
314+
#![allow(clippy::type_repetition_in_bounds)]
315+
#![allow(clippy::expl_impl_clone_on_copy)]
330316

331317
mod key;
332318
pub use self::key::Key;
319+
320+
/// Derive to implement the [`Key`] trait.
321+
///
322+
/// This derive implements the [`Key`] trait for a given type.
323+
///
324+
/// The [`Key`] trait is what allows `fixed_map` to set up storage for a type
325+
/// that will be the key in a fixed map.
326+
///
327+
/// > Note: this requires the `::fixed_map` crate to be in scope when it's
328+
/// > derived.
329+
///
330+
/// # Container attributes
331+
///
332+
/// #### `#[key(bitset)]`
333+
///
334+
/// This ensures that backing storage is performed with a bitset when used with
335+
/// a [`Set`].
336+
///
337+
/// ```rust
338+
/// use fixed_map::{Key, Set};
339+
///
340+
/// #[derive(Clone, Copy, Key)]
341+
/// pub enum Regular {
342+
/// First,
343+
/// Second,
344+
/// Third,
345+
/// }
346+
///
347+
/// #[derive(Clone, Copy, Key)]
348+
/// #[key(bitset)]
349+
/// pub enum Bits {
350+
/// First,
351+
/// Second,
352+
/// Third,
353+
/// }
354+
///
355+
/// // Normal storage uses an array of booleans:
356+
/// assert_eq!(std::mem::size_of::<Set<Regular>>(), 3);
357+
///
358+
/// // Bitset storage uses a single u8 (or other appropriate type based on size):
359+
/// assert_eq!(std::mem::size_of::<Set<Bits>>(), 1);
360+
/// ```
361+
///
362+
/// > **Warning:** not all operations will be implemented when this attributes
363+
/// > is present, so some container methods might not work.
364+
///
365+
/// # Guide
366+
///
367+
/// Given the following enum:
368+
///
369+
/// ```rust
370+
/// use fixed_map::Key;
371+
///
372+
/// #[derive(Clone, Copy, Key)]
373+
/// pub enum MyKey {
374+
/// First,
375+
/// Second,
376+
/// Third,
377+
/// }
378+
/// ```
379+
///
380+
/// It performs the following expansion:
381+
///
382+
/// ```rust,no_compile,no_run
383+
/// use fixed_map::Key;
384+
///
385+
/// #[derive(Clone, Copy)]
386+
/// pub enum MyKey {
387+
/// First,
388+
/// Second,
389+
/// Third,
390+
/// }
391+
///
392+
/// /// Build a storage struct containing an item for each key:
393+
/// pub struct MyKeyMapStorage<V> {
394+
/// /// Storage for `MyKey::First`.
395+
/// f1: Option<V>,
396+
/// /// Storage for `MyKey::Second`.
397+
/// f2: Option<V>,
398+
/// /// Storage for `MyKey::Third`.
399+
/// f3: Option<V>,
400+
/// }
401+
///
402+
/// /// Build a storage struct containing an element for each key:
403+
/// pub struct MyKeySetStorage {
404+
/// data: [bool; 3],
405+
/// }
406+
///
407+
/// /// Implement map storage for key.
408+
/// impl<V> fixed_map::map::storage::MapStorage<MyKey, V> for MyKeyMapStorage<V> {
409+
/// fn get(&self, key: MyKey) -> Option<&V> {
410+
/// match key {
411+
/// MyKey::First => self.f1.as_ref(),
412+
/// MyKey::Second => self.f2.as_ref(),
413+
/// MyKey::Third => self.f3.as_ref(),
414+
/// }
415+
/// }
416+
///
417+
/// /* skipped */
418+
/// }
419+
///
420+
/// /// Implement set storage for key.
421+
/// impl fixed_map::set::storage::SetStorage<MyKey> for MyKeySetStorage {
422+
/// fn contains(&self, key: MyKey) -> Option<&V> {
423+
/// let [a, b, c] = &self.data;
424+
///
425+
/// match key {
426+
/// MyKey::First => a,
427+
/// MyKey::Second => b,
428+
/// MyKey::Third => c,
429+
/// }
430+
/// }
431+
///
432+
/// /* skipped */
433+
/// }
434+
///
435+
/// impl Default for MyKeyMapStorage<V> {
436+
/// fn default() -> Self {
437+
/// Self {
438+
/// f1: None,
439+
/// f2: None,
440+
/// f3: None,
441+
/// }
442+
/// }
443+
/// }
444+
///
445+
/// impl Default for MyKeySetStorage {
446+
/// fn default() -> Self {
447+
/// Self {
448+
/// data: [false, false, false]
449+
/// }
450+
/// }
451+
/// }
452+
///
453+
/// /// Implement the Key trait to point out storage.
454+
/// impl fixed_map::Key for MyKey {
455+
/// type MapStorage<V> = MyKeyMapStorage<V>;
456+
/// type SetStorage = MyKeySetStorage;
457+
/// }
458+
/// ```
333459
#[doc(inline)]
334-
pub use fixed_map_derive::*;
460+
pub use fixed_map_derive::Key;
335461

336462
pub mod map;
337463
#[doc(inline)]

src/map/storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Module that defines the [`Storage`] trait.
1+
//! Module that defines the [`MapStorage`] trait.
22
33
mod boolean;
44
pub(crate) use self::boolean::BooleanMapStorage;

0 commit comments

Comments
 (0)