Skip to content

Commit

Permalink
Impl ListIter for im::OrdMap (#1641)
Browse files Browse the repository at this point in the history
* adding ListIter implementations and examples

* Changed example name, added modifying via buttons.

* handling changes of Key for OrdMap and HashMap

* Added lejero user, pull request detail

* changelog edit

* github spelling mistake

* change log overwrite

* formatting, adhering to standards

* adding changelog entry

* Added PR

* Added web examples entry for list_sources.rs

* Removed HashMap ListIter implementation.

* Update CHANGELOG.md

Co-authored-by: Colin Rofls <[email protected]>

* Update druid/examples/list_sources.rs

Co-authored-by: Colin Rofls <[email protected]>

* Removed Unnecessary Clone

* Removed HashMap Key and Value impl for ListIter and list_sources.rs example

Co-authored-by: Colin Rofls <[email protected]>
  • Loading branch information
Lejero and cmyr authored Mar 12, 2021
1 parent 834f94e commit d3f3550
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You can find its changes [documented below](#070---2021-01-01).
- Shell: windows implementation from content_insets ([#1592] by [@HoNile])
- Shell: IME API and macOS IME implementation ([#1619] by [@lord])
- Scroll::content_must_fill and a few other new Scroll methods ([#1635] by [@cmyr])
- Added ListIter implementations for OrdMap ([#1641] by [@Lejero])

### Changed

Expand Down Expand Up @@ -426,6 +427,7 @@ Last release without a changelog :(
[@HoNile]: https://github.com/HoNile
[@SecondFlight]: https://github.com/SecondFlight
[@lord]: https://github.com/lord
[@Lejero]: https://github.com/Lejero

[#599]: https://github.com/linebender/druid/pull/599
[#611]: https://github.com/linebender/druid/pull/611
Expand Down Expand Up @@ -632,6 +634,7 @@ Last release without a changelog :(
[#1619]: https://github.com/linebender/druid/pull/1619
[#1634]: https://github.com/linebender/druid/pull/1634
[#1635]: https://github.com/linebender/druid/pull/1635
[#1641]: https://github.com/linebender/druid/pull/1641

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0
Expand Down
34 changes: 32 additions & 2 deletions druid/src/widget/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::f64;
use std::sync::Arc;

#[cfg(feature = "im")]
use crate::im::Vector;
use crate::im::{OrdMap, Vector};

use crate::kurbo::{Point, Rect, Size};

Expand Down Expand Up @@ -97,7 +97,6 @@ pub trait ListIter<T>: Data {
/// Return data length.
fn data_len(&self) -> usize;
}

#[cfg(feature = "im")]
impl<T: Data> ListIter<T> for Vector<T> {
fn for_each(&self, mut cb: impl FnMut(&T, usize)) {
Expand All @@ -117,6 +116,37 @@ impl<T: Data> ListIter<T> for Vector<T> {
}
}

//An implementation for ListIter<(K, V)> has been ommitted due to problems
//with how the List Widget handles the reordering of its data.
#[cfg(feature = "im")]
impl<K, V> ListIter<V> for OrdMap<K, V>
where
K: Data + Ord,
V: Data,
{
fn for_each(&self, mut cb: impl FnMut(&V, usize)) {
for (i, item) in self.iter().enumerate() {
let ret = (item.0.to_owned(), item.1.to_owned());
cb(&ret.1, i);
}
}

fn for_each_mut(&mut self, mut cb: impl FnMut(&mut V, usize)) {
for (i, item) in self.clone().iter().enumerate() {
let mut ret = item.1.clone();
cb(&mut ret, i);

if !item.1.same(&ret) {
self[&item.0] = ret;
}
}
}

fn data_len(&self) -> usize {
self.len()
}
}

// S == shared data type
#[cfg(feature = "im")]
impl<S: Data, T: Data> ListIter<(S, T)> for (S, Vector<T>) {
Expand Down

0 comments on commit d3f3550

Please sign in to comment.