Struct constructivism::core::Prop

source ·
pub struct Prop<H, T> { /* private fields */ }

Implementations§

source§

impl<H, T> Prop<H, T>

source

pub fn new( getter: fn(_: &H) -> Value<'_, T>, setter: fn(_: &mut H, _: T) ) -> Self

source

pub fn get<'a>(&self, host: &'a H) -> Value<'a, T>

Examples found in repository?
examples/tutorial.rs (line 210)
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
fn button_props() {
    let (mut button, mut input, mut rect, mut node) = construct!(Button);

    // You can access to props knowing only the top-level Construct
    // Prop<Node, (f32, f32)>
    let pos = prop!(Button.position);
    // Prop<Rect, (f32, f32)>
    let size = prop!(Button.size);
    // Prop<Input, bool>
    let disabled = prop!(Button.disabled);
    // Prop<Button, bool>
    let pressed = prop!(Button.pressed);

    // You can read props. You have to pass exact item to the get()
    let x = pos.get(&node).as_ref().0;
    let w = size.get(&rect).as_ref().0;
    let is_disabled = *disabled.get(&input).as_ref();
    let is_pressed = *pressed.get(&button).as_ref();
    assert_eq!(0., x);
    assert_eq!(0., w);
    assert_eq!(false, is_disabled);
    assert_eq!(false, is_pressed);

    // You can set props. You have to pass exact item to set()
    pos.set(&mut node, (1., 1.));
    size.set(&mut rect, (10., 10.));
    disabled.set(&mut input, true);
    pressed.set(&mut button, true);
    assert_eq!(node.position.0, 1.);
    assert_eq!(rect.size.0, 10.);
    assert_eq!(input.disabled, true);
    assert_eq!(button.pressed, true);
}
source

pub fn set(&self, host: &mut H, value: T)

Examples found in repository?
examples/tutorial.rs (line 220)
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
fn button_props() {
    let (mut button, mut input, mut rect, mut node) = construct!(Button);

    // You can access to props knowing only the top-level Construct
    // Prop<Node, (f32, f32)>
    let pos = prop!(Button.position);
    // Prop<Rect, (f32, f32)>
    let size = prop!(Button.size);
    // Prop<Input, bool>
    let disabled = prop!(Button.disabled);
    // Prop<Button, bool>
    let pressed = prop!(Button.pressed);

    // You can read props. You have to pass exact item to the get()
    let x = pos.get(&node).as_ref().0;
    let w = size.get(&rect).as_ref().0;
    let is_disabled = *disabled.get(&input).as_ref();
    let is_pressed = *pressed.get(&button).as_ref();
    assert_eq!(0., x);
    assert_eq!(0., w);
    assert_eq!(false, is_disabled);
    assert_eq!(false, is_pressed);

    // You can set props. You have to pass exact item to set()
    pos.set(&mut node, (1., 1.));
    size.set(&mut rect, (10., 10.));
    disabled.set(&mut input, true);
    pressed.set(&mut button, true);
    assert_eq!(node.position.0, 1.);
    assert_eq!(rect.size.0, 10.);
    assert_eq!(input.disabled, true);
    assert_eq!(button.pressed, true);
}

// 4.2 **Expand props**: If you have field with Construct type, you can access this fields props as well:

#[derive(Construct, Default)]
pub struct Vec2 {
    x: f32,
    y: f32,
}

#[derive(Construct)]
pub struct Node2d {
    #[prop(construct)] // You have to mark expandable props with #[prop(construct)]
    position: Vec2,
}

fn modify_position_x() {
    let mut node = construct!(Node2d);
    assert_eq!(node.position.x, 0.);
    assert_eq!(node.position.y, 0.);

    // Prop<Node2d, f32>
    let x = prop!(Node2d.position.x);

    x.set(&mut node, 100.);
    assert_eq!(node.position.x, 100.);
    assert_eq!(node.position.y, 0.);
}

// ### Custom Constructors

// 5.1  **Custom Constructors**: Sometimes you may want to derive Construct for a foreign type
//      or provide a custom constructor/props for your type. You can use `derive_construct!` for
//      this purpose:
pub struct ProgressBar {
    min: f32,
    val: f32,
    max: f32,
}
impl ProgressBar {
    pub fn min(&self) -> f32 {
        self.min
    }
    pub fn set_min(&mut self, min: f32) {
        self.min = min;
        if self.max < min {
            self.max = min;
        }
        if self.val < min {
            self.val = min;
        }
    }
    pub fn max(&self) -> f32 {
        self.max
    }
    pub fn set_max(&mut self, max: f32) {
        self.max = max;
        if self.min > max {
            self.min = max;
        }
        if self.val > max {
            self.val = max;
        }
    }
    pub fn val(&self) -> f32 {
        self.val
    }
    pub fn set_val(&mut self, val: f32) {
        self.val = val.max(self.min).min(self.max)
    }
}

derive_construct! {
    // Sequence
    seq => ProgressBar -> Rect;

    // Constructor, all params with defult values
    construct => (min: f32 = 0., max: f32 = 1., val: f32 = 0.) -> {
        if max < min {
            max = min;
        }
        val = val.min(max).max(min);
        Self { min, val, max }
    };

    // Props using getters and setters
    props => {
        min: f32 = [min, set_min];
        max: f32 = [max, set_max];
        val: f32 = [val, set_val];
    };
}

// 5.2  **Using Custom Constructors**: The provided constructor will be called when creating
//      instances:
fn create_progress_bar() {
    let (pb, _, _) = construct!(ProgressBar { .val: 100. });
    assert_eq!(pb.min, 0.);
    assert_eq!(pb.max, 1.);
    assert_eq!(pb.val, 1.);
}

// 5.3  **Custom Construct Props**: In the example above `derive_construct!` declares props using
//      getters and setters. This setters and getters are called when you use `Prop::get`
//      and `Prop::set`
fn modify_progress_bar() {
    let (mut pb, _, _) = construct!(ProgressBar);
    let min = prop!(ProgressBar.min);
    let val = prop!(ProgressBar.val);
    let max = prop!(ProgressBar.max);

    assert_eq!(pb.val, 0.);

    val.set(&mut pb, 2.);
    assert_eq!(pb.val, 1.0); //because default for max = 1.0

    min.set(&mut pb, 5.);
    max.set(&mut pb, 10.);
    assert_eq!(pb.min, 5.);
    assert_eq!(pb.val, 5.);
    assert_eq!(pb.max, 10.);
}
source

pub fn getter(&self) -> fn(_: &H) -> Value<'_, T>

source

pub fn setter(&self) -> fn(_: &mut H, _: T)

Trait Implementations§

source§

impl<H, T> Clone for Prop<H, T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<H: Copy, T: Copy> Copy for Prop<H, T>

Auto Trait Implementations§

§

impl<H, T> RefUnwindSafe for Prop<H, T>

§

impl<H, T> Send for Prop<H, T>

§

impl<H, T> Sync for Prop<H, T>

§

impl<H, T> Unpin for Prop<H, T>

§

impl<H, T> UnwindSafe for Prop<H, T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.