-
Notifications
You must be signed in to change notification settings - Fork 71
Add documentation for implementing EnumerableSet #786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
99ac5c3
80719cf
9fdc13f
2e7a3a4
b360335
7fe7c36
0d14b15
581d9ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,13 +2,6 @@ | |||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
pub mod element; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
/// Sets have the following properties: | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// * Elements are added, removed, and checked for existence in constant | ||||||||||||||||||||||||||||||||||||||||||
/// time (O(1)). | ||||||||||||||||||||||||||||||||||||||||||
/// * Elements are enumerated in O(n). No guarantees are made on the | ||||||||||||||||||||||||||||||||||||||||||
/// ordering. | ||||||||||||||||||||||||||||||||||||||||||
/// * Set can be cleared (all elements removed) in O(n). | ||||||||||||||||||||||||||||||||||||||||||
use alloc::{vec, vec::Vec}; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
use alloy_primitives::{uint, U256}; | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -18,7 +11,63 @@ use stylus_sdk::{ | |||||||||||||||||||||||||||||||||||||||||
storage::{StorageMap, StorageType, StorageU256, StorageVec}, | ||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
/// State of an [`EnumerableSet`] contract. | ||||||||||||||||||||||||||||||||||||||||||
/// Sets have the following properties: | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// * Elements are added, removed, and checked for existence in constant time | ||||||||||||||||||||||||||||||||||||||||||
/// (O(1)). | ||||||||||||||||||||||||||||||||||||||||||
/// * Elements are enumerated in O(n). No guarantees are made on the ordering. | ||||||||||||||||||||||||||||||||||||||||||
/// * Set can be cleared (all elements removed) in O(n). | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// ## Usage | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+14
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// `EnumerableSet` works with the following primitive types out of the box: | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// * [`alloy_primitives::Address`] - Ethereum addresses | ||||||||||||||||||||||||||||||||||||||||||
/// * [`alloy_primitives::B256`] - 256-bit byte arrays | ||||||||||||||||||||||||||||||||||||||||||
/// * [`alloy_primitives::U8`] - 8-bit unsigned integers | ||||||||||||||||||||||||||||||||||||||||||
/// * [`alloy_primitives::U16`] - 16-bit unsigned integers | ||||||||||||||||||||||||||||||||||||||||||
/// * [`alloy_primitives::U32`] - 32-bit unsigned integers | ||||||||||||||||||||||||||||||||||||||||||
/// * [`alloy_primitives::U64`] - 64-bit unsigned integers | ||||||||||||||||||||||||||||||||||||||||||
/// * [`alloy_primitives::U128`] - 128-bit unsigned integers | ||||||||||||||||||||||||||||||||||||||||||
/// * [`alloy_primitives::U256`] - 256-bit unsigned integers | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// ```rust | ||||||||||||||||||||||||||||||||||||||||||
/// extern crate alloc; | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// use alloy_primitives::{Address, U256}; | ||||||||||||||||||||||||||||||||||||||||||
/// use stylus_sdk::prelude::*; | ||||||||||||||||||||||||||||||||||||||||||
/// use openzeppelin_stylus::utils::structs::enumerable_set::EnumerableSet; | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// #[storage] | ||||||||||||||||||||||||||||||||||||||||||
/// struct MyContract { | ||||||||||||||||||||||||||||||||||||||||||
/// whitelist: EnumerableSet<Address>, | ||||||||||||||||||||||||||||||||||||||||||
/// } | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// #[public] | ||||||||||||||||||||||||||||||||||||||||||
/// impl MyContract { | ||||||||||||||||||||||||||||||||||||||||||
/// fn add_to_whitelist(&mut self, address: Address) -> bool { | ||||||||||||||||||||||||||||||||||||||||||
/// self.whitelist.add(address) | ||||||||||||||||||||||||||||||||||||||||||
/// } | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// fn remove_from_whitelist(&mut self, address: Address) -> bool { | ||||||||||||||||||||||||||||||||||||||||||
/// self.whitelist.remove(address) | ||||||||||||||||||||||||||||||||||||||||||
/// } | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// fn is_whitelisted(&self, address: Address) -> bool { | ||||||||||||||||||||||||||||||||||||||||||
/// self.whitelist.contains(address) | ||||||||||||||||||||||||||||||||||||||||||
/// } | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// fn get_whitelist_size(&self) -> U256 { | ||||||||||||||||||||||||||||||||||||||||||
/// self.whitelist.length() | ||||||||||||||||||||||||||||||||||||||||||
/// } | ||||||||||||||||||||||||||||||||||||||||||
/// } | ||||||||||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// ## Custom Storage Types | ||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||
/// You can implement `EnumerableSet` for your own storage types by implementing | ||||||||||||||||||||||||||||||||||||||||||
/// the `Element` and `Accessor` traits. See [`element.rs`] for trait | ||||||||||||||||||||||||||||||||||||||||||
/// definitions and the documentation for comprehensive examples. | ||||||||||||||||||||||||||||||||||||||||||
#[storage] | ||||||||||||||||||||||||||||||||||||||||||
pub struct EnumerableSet<T: Element> { | ||||||||||||||||||||||||||||||||||||||||||
/// Values in the set. | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -188,6 +237,7 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||
use stylus_sdk::prelude::TopLevelStorage; | ||||||||||||||||||||||||||||||||||||||||||
use alloy_primitives::private::proptest::{prop_assert, prop_assert_eq, proptest}; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't seem to find this one, on lines provided it is not after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you didn't push local changes? |
||||||||||||||||||||||||||||||||||||||||||
unsafe impl TopLevelStorage for $set_type {} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
#[public] | ||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the mod-level doc to: