Skip to content

Commit

Permalink
add code example and diagram
Browse files Browse the repository at this point in the history
  • Loading branch information
geeklint committed May 1, 2023
1 parent 5193d54 commit 50a7e3b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib-example.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,46 @@
//! `remove` method. This requirement is easy to fulfil for applications where
//! a value is only removed from the set by a logical "owner" which knows that
//! it previously inserted a value.
//!
//! # Example
//!
//! The following code inserts some values into the set, then removes one of
//! them, and then spawns a second thread that waits to insert into the set.
//!
#![cfg_attr(
feature = "std",
doc = "
```
# use std::{sync::Arc, any::TypeId};
# use typeid_set::TypeIdSet;
# unsafe {
struct A;
struct B;
struct C;
let set: Arc<TypeIdSet> = Arc::default();
set.try_insert(TypeId::of::<C>());
set.try_insert(TypeId::of::<B>());
set.try_insert(TypeId::of::<A>());
set.remove(TypeId::of::<A>());
let set2 = set.clone();
# let handle =
std::thread::spawn(move || {
set2.wait_to_insert(TypeId::of::<B>());
});
# set.remove(TypeId::of::<B>()); // avoid a deadlock in the example
# handle.join();
# }
```
"
)]
//!
//! After this code has been run, we can expect the data structure to look like
//! this:
//!
//! <div style="background-color: white">
#![doc=include_str!("lib-example.svg")]
//! </div>

#![cfg_attr(not(feature = "std"), no_std)]
#![deny(clippy::all, clippy::pedantic)]
Expand Down

0 comments on commit 50a7e3b

Please sign in to comment.