mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 13:59:08 +00:00
Add basic roc_std Dict and Set implementations
This commit is contained in:
parent
97fef48864
commit
ec0e80f5d2
3 changed files with 112 additions and 0 deletions
35
crates/roc_std/src/roc_set.rs
Normal file
35
crates/roc_std/src/roc_set.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use crate::roc_dict::RocDict;
|
||||
use core::{
|
||||
fmt::{self, Debug},
|
||||
hash::Hash,
|
||||
};
|
||||
|
||||
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct RocSet<T>(RocDict<T, ()>);
|
||||
|
||||
impl<T> RocSet<T> {
|
||||
#[allow(unused)]
|
||||
pub fn with_capacity(capacity: usize) -> Self {
|
||||
Self(RocDict::with_capacity(capacity))
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
||||
self.0.iter_keys()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash> RocSet<T> {
|
||||
#[allow(unused)]
|
||||
pub fn from_iter<I: Iterator<Item = T>>(src: I) -> Self {
|
||||
Self(RocDict::from_iter(src.map(|elem| (elem, ()))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Debug> Debug for RocSet<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("RocSet ")?;
|
||||
|
||||
f.debug_set().entries(self.iter()).finish()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue