Skip to content

Commit

Permalink
Add examples to the various sets
Browse files Browse the repository at this point in the history
  • Loading branch information
malobre committed May 30, 2022
1 parent 4e54481 commit c3dc0c6
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,20 +376,89 @@ impl_erased_set! {
/// A set of erased types.
///
/// This set can store a single instance of any type that implements [`Any`](::core::any::Any).
///
/// ## Example
///
/// ```
/// # #[derive(Debug, PartialEq)]
/// # struct ClickEvent(u32, u32);
/// # #[derive(Debug, PartialEq)]
/// # struct KeyDownEvent(char);
/// #
/// use erased_set::ErasedSet;
///
/// let mut set = ErasedSet::new();
/// set.insert(ClickEvent(128, 256));
/// set.insert(KeyDownEvent('z'));
///
/// assert_eq!(set.get::<ClickEvent>(), Some(&ClickEvent(128, 256)));
///
/// assert_eq!(set.insert(KeyDownEvent('e')), Some(KeyDownEvent('z')));
///
/// set.remove::<ClickEvent>();
///
/// assert_eq!(set.len(), 1);
/// ```
#[derive(Debug, Default)]
pub struct ErasedSet: Any;
}

#[cfg(feature = "send")]
impl_erased_set! {
/// Like [`ErasedSet`] but with a [`Send`] bound.
///
/// ## Example
///
/// ```
/// # #[derive(Debug, PartialEq)]
/// # struct ClickEvent(u32, u32);
/// # #[derive(Debug, PartialEq)]
/// # struct KeyDownEvent(char);
/// #
/// use erased_set::ErasedSendSet;
///
/// let mut set = ErasedSendSet::new();
/// set.insert(ClickEvent(128, 256));
/// set.insert(KeyDownEvent('z'));
///
/// assert_eq!(set.get::<ClickEvent>(), Some(&ClickEvent(128, 256)));
///
/// assert_eq!(set.insert(KeyDownEvent('e')), Some(KeyDownEvent('z')));
///
/// set.remove::<ClickEvent>();
///
/// assert_eq!(set.len(), 1);
/// ```
#[derive(Debug, Default)]
pub struct ErasedSendSet: Any + Send;
}

#[cfg(feature = "sync")]
impl_erased_set! {
/// Like [`ErasedSet`] but with a [`Send`] + [`Sync`] bound.
///
/// ## Example
///
/// ```
/// # #[derive(Debug, PartialEq)]
/// # struct ClickEvent(u32, u32);
/// # #[derive(Debug, PartialEq)]
/// # struct KeyDownEvent(char);
/// #
/// use erased_set::ErasedSyncSet;
///
/// let mut set = ErasedSyncSet::new();
/// set.insert(ClickEvent(128, 256));
/// set.insert(KeyDownEvent('z'));
///
/// assert_eq!(set.get::<ClickEvent>(), Some(&ClickEvent(128, 256)));
///
/// assert_eq!(set.insert(KeyDownEvent('e')), Some(KeyDownEvent('z')));
///
/// set.remove::<ClickEvent>();
///
/// assert_eq!(set.len(), 1);
/// ```
#[derive(Debug, Default)]
pub struct ErasedSyncSet: Any + Send + Sync;
}

0 comments on commit c3dc0c6

Please sign in to comment.