#option-t #attributes #u32

projection

Projection into Option<T>

2 releases

0.1.1 Sep 20, 2020
0.1.0 Sep 20, 2020

#10 in #option-t

BSL-1.0 license

4KB

Projection

Documentation


lib.rs:

projection provides you easy access struct fields of a Option.

Examples

To use projection, you first need to annotate your type with the projection attribute

use ::projection::projection;
#[projection]
struct AType {
    a: u32,
    b: u32
}

Then, you can use project() to access fields of AType from a Option<AType>

use ::projection::prelude::*;

let var: Option<AType> = None;
let var = var.project();
assert_eq!(var.a, None);

let var = Some(AType { a: 1, b: 1 });
let var = var.project();
assert_eq!(var.a, Some(1));

// You can choose to borrow the values
let mut var = Some(AType { a: 1, b: 1 });
let var = var.as_mut().project();
assert_eq!(var.a, Some(&mut 1));

let var = Some(AType { a: 1, b: 1 });
let var = var.as_ref().project();
assert_eq!(var.a, Some(&1));

Dependencies

~2MB
~42K SLoC