Skip to content

Commit

Permalink
Unit lens.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjwittams committed Sep 14, 2020
1 parent 2d08faf commit 23527a8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ You can find its changes [documented below](#060---2020-06-01).
- `LineBreaking` enum allows configuration of label line-breaking ([#1195] by [@cmyr])
- `TextAlignment` support in `TextLayout` and `Label` ([#1210] by [@cmyr])`
- `Button::from_label` to construct a `Button` with a provided `Label`. ([#1226] by [@ForLoveOfCats])
- Lens: Added Unit lens for type erased / display only widgets that do not need data. ([#1232] by [@rjwittams])

### Changed

Expand Down Expand Up @@ -449,6 +450,7 @@ Last release without a changelog :(
[#1210]: https://github.com/linebender/druid/pull/1210
[#1214]: https://github.com/linebender/druid/pull/1214
[#1226]: https://github.com/linebender/druid/pull/1226
[#1232]: https://github.com/linebender/druid/pull/1232

[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0
Expand Down
26 changes: 26 additions & 0 deletions druid/src/lens/lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,29 @@ where
v
}
}

/// A `Lens` that always yields ().
///
/// This is useful when you wish to have a display only widget, require a type-erased widget, or
/// obtain app data out of band and ignore your input. (E.g sub-windows)
#[derive(Debug, Copy, Clone)]
pub struct Unit<T> {
phantom_t: PhantomData<T>,
}

impl<T> Default for Unit<T> {
fn default() -> Self {
Unit {
phantom_t: Default::default(),
}
}
}

impl<T> Lens<T, ()> for Unit<T> {
fn with<V, F: FnOnce(&()) -> V>(&self, _data: &T, f: F) -> V {
f(&())
}
fn with_mut<V, F: FnOnce(&mut ()) -> V>(&self, _data: &mut T, f: F) -> V {
f(&mut ())
}
}
2 changes: 1 addition & 1 deletion druid/src/lens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
#[allow(clippy::module_inception)]
mod lens;
pub use lens::{Deref, Field, Id, InArc, Index, Map, Ref, Then};
pub use lens::{Deref, Field, Id, InArc, Index, Map, Ref, Then, Unit};
#[doc(hidden)]
pub use lens::{Lens, LensExt, LensWrap};

0 comments on commit 23527a8

Please sign in to comment.