Skip to content

Commit

Permalink
Implement IntoIterator for &Attributes and Extend for Attributes (#386)
Browse files Browse the repository at this point in the history
The original implementation of Attributes had an `iter` method but it
did not implement `FromIterator` for `&Attributes`, making it so you
couldn't use `for (key, value) in &attributes` and had to use `for (key,
value) in attributes.iter()`. This changes that.

Additionally, `Extend` is a nice quality of life trait for containers so
it's implemented here.
  • Loading branch information
Dekkonot committed Feb 1, 2024
1 parent b521e0e commit d960c04
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
7 changes: 6 additions & 1 deletion rbx_types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# rbx_types Changelog

## Unreleased Changes
* Implement `IntoIterator` for `&Attributes`. ([#386])
* Implement `Extend<(String, Variant)>` for `Attributes`. ([#386])

[#386]: https://github.com/rojo-rbx/rbx-dom/pull/386

## 1.8.0 (2024-01-16)
* Add `len` and `is_empty` methods to `Attributes` struct. ([#377])

[#377]: https://github.com/rojo-rbx/rbx-dom/pull/377

## 1.7.0 (2023-10-03)
* Implemented `FromStr` for `TerrainMaterials`. ([#354])
* `MaterialColorsError` and `UniqueIdError` are no longer publicly exposed. ([#355])
Expand All @@ -15,7 +21,6 @@
[#355]: https://github.com/rojo-rbx/rbx-dom/pull/355
[#358]: https://github.com/rojo-rbx/rbx-dom/pull/358
[#363]: https://github.com/rojo-rbx/rbx-dom/pull/363
[#377]: https://github.com/rojo-rbx/rbx-dom/pull/377

## 1.6.0 (2023-08-09)
* Added support for `UniqueId` values. ([#271])
Expand Down
38 changes: 36 additions & 2 deletions rbx_types/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ impl Attributes {
}

/// Returns an iterator of borrowed attributes.
pub fn iter(&self) -> impl Iterator<Item = (&String, &Variant)> {
self.data.iter()
#[inline]
pub fn iter(&self) -> AttributesIter<'_> {
AttributesIter {
iter: self.data.iter(),
}
}

/// Returns the number of attributes.
Expand All @@ -92,6 +95,12 @@ impl Attributes {
}
}

impl Extend<(String, Variant)> for Attributes {
fn extend<T: IntoIterator<Item = (String, Variant)>>(&mut self, iter: T) {
self.data.extend(iter)
}
}

impl IntoIterator for Attributes {
type IntoIter = AttributesIntoIter;
type Item = (String, Variant);
Expand All @@ -103,6 +112,17 @@ impl IntoIterator for Attributes {
}
}

impl<'a> IntoIterator for &'a Attributes {
type IntoIter = AttributesIter<'a>;
type Item = (&'a String, &'a Variant);

fn into_iter(self) -> Self::IntoIter {
AttributesIter {
iter: self.data.iter(),
}
}
}

impl FromIterator<(String, Variant)> for Attributes {
fn from_iter<T: IntoIterator<Item = (String, Variant)>>(iter: T) -> Self {
Self {
Expand All @@ -125,6 +145,20 @@ impl Iterator for AttributesIntoIter {
}
}

/// A borrowed iterator over the entries of an `Attributes`.
/// This is created by [`Attributes::iter`].
pub struct AttributesIter<'a> {
iter: btree_map::Iter<'a, String, Variant>,
}

impl<'a> Iterator for AttributesIter<'a> {
type Item = (&'a String, &'a Variant);

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit d960c04

Please sign in to comment.