Skip to content

Commit

Permalink
Supports accessing nested fields with lens macro (#1764)
Browse files Browse the repository at this point in the history
Now this is valid

struct Foo { x: Bar }
struct Bar { y: [i32; 10] }
lens!(Foo, x.y[5]);
  • Loading branch information
maan2003 committed May 13, 2021
1 parent 6e028a9 commit dcb5250
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ You can find its changes [documented below](#070---2021-01-01).
- GTK: added support for `content_insets` ([#1722] by [@jneem])
- `chrono` feature with `Data` support for [chrono](https://docs.rs/chrono/) types ([#1743] by [@r-ml])
- Text input handles Delete key ([#1746] by [@bjorn])
- `lens` macro can access nested fields ([#1764] by [@Maan2003])

### Changed

Expand Down Expand Up @@ -719,6 +720,7 @@ Last release without a changelog :(
[#1755]: https://github.com/linebender/druid/pull/1755
[#1756]: https://github.com/linebender/druid/pull/1756
[#1761]: https://github.com/linebender/druid/pull/1761
[#1764]: https://github.com/linebender/druid/pull/1764
[#1772]: https://github.com/linebender/druid/pull/1772

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
Expand Down
8 changes: 5 additions & 3 deletions druid/src/lens/lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,20 @@ where
/// This is a convenience macro for constructing `Field` lenses for fields or indexable elements.
///
/// ```
/// struct Foo { x: u32 }
/// struct Foo { x: Bar }
/// struct Bar { y: [i32; 10] }
/// let lens = druid::lens!(Foo, x);
/// let lens = druid::lens!((u32, bool), 1);
/// let lens = druid::lens!([u8], [4]);
/// let lens = druid::lens!(Foo, x.y[5]);
/// ```
#[macro_export]
macro_rules! lens {
($ty:ty, [$index:expr]) => {
$crate::lens::Field::new::<$ty, _>(move |x| &x[$index], move |x| &mut x[$index])
};
($ty:ty, $field:tt) => {
$crate::lens::Field::new::<$ty, _>(move |x| &x.$field, move |x| &mut x.$field)
($ty:ty, $($field:tt)*) => {
$crate::lens::Field::new::<$ty, _>(move |x| &x.$($field)*, move |x| &mut x.$($field)*)
};
}

Expand Down

0 comments on commit dcb5250

Please sign in to comment.