diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-19 20:49:01 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-19 20:49:01 +0200 |
| commit | 77dac270a8a99f24a6fc0eb9e92256bcc07c586c (patch) | |
| tree | 8e240b798a5c1aabd77c823e65828f3c6d2557f1 /src/compute/table.rs | |
| parent | 6d7e7d945b315469b80bca3466a96534b2a17639 (diff) | |
Make compute functions possible 💻
Ships with the amazing new `rgb` function!
Diffstat (limited to 'src/compute/table.rs')
| -rw-r--r-- | src/compute/table.rs | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/src/compute/table.rs b/src/compute/table.rs index e8c4b307..bb71d4f2 100644 --- a/src/compute/table.rs +++ b/src/compute/table.rs @@ -115,6 +115,17 @@ impl<V> Table<V> { self.lowest_free += 1; } + /// Iterator over all borrowed keys and values. + pub fn iter(&self) -> impl Iterator<Item = (BorrowedKey, &V)> { + self.nums().map(|(&k, v)| (BorrowedKey::Num(k), v)) + .chain(self.strs().map(|(k, v)| (BorrowedKey::Str(k), v))) + } + + /// Iterate over all values in the table. + pub fn values(&self) -> impl Iterator<Item = &V> { + self.nums().map(|(_, v)| v).chain(self.strs().map(|(_, v)| v)) + } + /// Iterate over the number key-value pairs. pub fn nums(&self) -> std::collections::btree_map::Iter<u64, V> { self.nums.iter() @@ -125,9 +136,16 @@ impl<V> Table<V> { self.strs.iter() } - /// Iterate over all values in the table. - pub fn values(&self) -> impl Iterator<Item = &V> { - self.nums().map(|(_, v)| v).chain(self.strs().map(|(_, v)| v)) + /// Move into an owned iterator over owned keys and values. + pub fn into_iter(self) -> impl Iterator<Item = (OwnedKey, V)> { + self.nums.into_iter().map(|(k, v)| (OwnedKey::Num(k), v)) + .chain(self.strs.into_iter().map(|(k, v)| (OwnedKey::Str(k), v))) + } + + /// Move into an owned iterator over all values in the table. + pub fn into_values(self) -> impl Iterator<Item = V> { + self.nums.into_iter().map(|(_, v)| v) + .chain(self.strs.into_iter().map(|(_, v)| v)) } /// Iterate over the number key-value pairs. @@ -139,12 +157,6 @@ impl<V> Table<V> { pub fn into_strs(self) -> std::collections::btree_map::IntoIter<String, V> { self.strs.into_iter() } - - /// Move into an owned iterator over all values in the table. - pub fn into_values(self) -> impl Iterator<Item = V> { - self.nums.into_iter().map(|(_, v)| v) - .chain(self.strs.into_iter().map(|(_, v)| v)) - } } impl<'a, K, V> Index<K> for Table<V> @@ -168,7 +180,7 @@ impl<V: Eq> Eq for Table<V> {} impl<V: PartialEq> PartialEq for Table<V> { fn eq(&self, other: &Self) -> bool { - self.nums().eq(other.nums()) && self.strs().eq(other.strs()) + self.iter().eq(other.iter()) } } @@ -218,6 +230,15 @@ pub enum OwnedKey { Str(String), } +impl From<BorrowedKey<'_>> for OwnedKey { + fn from(key: BorrowedKey<'_>) -> Self { + match key { + BorrowedKey::Num(num) => Self::Num(num), + BorrowedKey::Str(string) => Self::Str(string.to_string()), + } + } +} + impl From<u64> for OwnedKey { fn from(num: u64) -> Self { Self::Num(num) |
