Demand

Struct Demand 

Source
pub struct Demand<'a>(/* private fields */);
Expand description

A helper object for supplying data by type.

A data supplier supplies values by calling this type’s supply methods.

Implementations§

Source§

impl<'a> Demand<'a>

Source

pub fn supply_value<T>(&mut self, value: T) -> &mut Self
where T: 'static,

Supply a value or other type with only static lifetimes.

§Examples

Supplies an u8.

use supplier::{Supplier, Demand};

impl Supplier for SomeConcreteType {
    fn supply<'a>(&'a self, demand: &mut Demand<'a>) {
        demand.supply_value::<u8>(self.field);
    }
}
Source

pub fn supply_value_with<T>(&mut self, fulfil: impl FnOnce() -> T) -> &mut Self
where T: 'static,

Supply a value or other type with only static lifetimes computed using a closure.

§Examples

Supplies a String by cloning.

use supplier::{Supplier, Demand};

impl Supplier for SomeConcreteType {
    fn supply<'a>(&'a self, demand: &mut Demand<'a>) {
        demand.supply_value_with::<String>(|| self.field.clone());
    }
}
Source

pub fn supply_ref<T: ?Sized + 'static>(&mut self, value: &'a T) -> &mut Self

Supply a reference. The referee type must be bounded by 'static, but may be unsized.

§Examples

Supplies a reference to a field as a &str.

use supplier::{Supplier, Demand};

impl Supplier for SomeConcreteType {
    fn supply<'a>(&'a self, demand: &mut Demand<'a>) {
        demand.supply_ref::<str>(&self.field);
    }
}
Source

pub fn supply_ref_with<T: ?Sized + 'static>( &mut self, fulfil: impl FnOnce() -> &'a T, ) -> &mut Self

Supply a reference computed using a closure. The referee type must be bounded by 'static, but may be unsized.

§Examples

Supplies a reference to a field as a &str.

use supplier::{Supplier, Demand};

impl Supplier for SomeConcreteType {
    fn supply<'a>(&'a self, demand: &mut Demand<'a>) {
        demand.supply_ref_with::<str>(|| {
            if today_is_a_weekday() {
                &self.business
            } else {
                &self.party
            }
        });
    }
}
Source

pub fn would_be_satisfied_by_value_of<T>(&self) -> bool
where T: 'static,

Check if the Demand would be satisfied if supplied with a value of the specified type. If the type does not match or has already been supplied, returns false.

§Examples

Check if an u8 still needs to be supplied and then supplies it.

use supplier::{Supplier, Demand};

struct Parent(Option<u8>);

impl Supplier for Parent {
    fn supply<'a>(&'a self, demand: &mut Demand<'a>) {
        if let Some(v) = self.0 {
            demand.supply_value::<u8>(v);
        }
    }
}

struct Child {
    parent: Parent,
}

impl Child {
    // Pretend that this takes a lot of resources to evaluate.
    fn an_expensive_computation(&self) -> Option<u8> {
        Some(99)
    }
}

impl Supplier for Child {
    fn supply<'a>(&'a self, demand: &mut Demand<'a>) {
        // In general, we don't know if this call will supply
        // an `u8` value or not...
        self.parent.supply(demand);

        // ...so we check to see if the `u8` is needed before
        // we run our expensive computation.
        if demand.would_be_satisfied_by_value_of::<u8>() {
            if let Some(v) = self.an_expensive_computation() {
                demand.supply_value::<u8>(v);
            }
        }

        // The demand will be satisfied now, regardless of if
        // the parent supplied the value or we did.
        assert!(!demand.would_be_satisfied_by_value_of::<u8>());
    }
}

let parent = Parent(Some(42));
let child = Child { parent };
assert_eq!(Some(42), supplier::request_value::<u8>(&child));

let parent = Parent(None);
let child = Child { parent };
assert_eq!(Some(99), supplier::request_value::<u8>(&child));
Source

pub fn would_be_satisfied_by_ref_of<T>(&self) -> bool
where T: ?Sized + 'static,

Check if the Demand would be satisfied if supplied with a reference to a value of the specified type. If the type does not match or has already been supplied, returns false.

§Examples

Check if a &str still needs to be supplied and then supplies it.

use supplier::{Supplier, Demand};

struct Parent(Option<String>);

impl Supplier for Parent {
    fn supply<'a>(&'a self, demand: &mut Demand<'a>) {
        if let Some(v) = &self.0 {
            demand.supply_ref::<str>(v);
        }
    }
}

struct Child {
    parent: Parent,
    name: String,
}

impl Child {
    // Pretend that this takes a lot of resources to evaluate.
    fn an_expensive_computation(&self) -> Option<&str> {
        Some(&self.name)
    }
}

impl Supplier for Child {
    fn supply<'a>(&'a self, demand: &mut Demand<'a>) {
        // In general, we don't know if this call will supply
        // a `str` reference or not...
        self.parent.supply(demand);

        // ...so we check to see if the `&str` is needed before
        // we run our expensive computation.
        if demand.would_be_satisfied_by_ref_of::<str>() {
            if let Some(v) = self.an_expensive_computation() {
                demand.supply_ref::<str>(v);
            }
        }

        // The demand will be satisfied now, regardless of if
        // the parent supplied the reference or we did.
        assert!(!demand.would_be_satisfied_by_ref_of::<str>());
    }
}

let parent = Parent(Some("parent".into()));
let child = Child { parent, name: "child".into() };
assert_eq!(Some("parent"), supplier::request_ref::<str>(&child));

let parent = Parent(None);
let child = Child { parent, name: "child".into() };
assert_eq!(Some("child"), supplier::request_ref::<str>(&child));

Trait Implementations§

Source§

impl<'a> Debug for Demand<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !Freeze for Demand<'a>

§

impl<'a> !RefUnwindSafe for Demand<'a>

§

impl<'a> !Send for Demand<'a>

§

impl<'a> !Sized for Demand<'a>

§

impl<'a> !Sync for Demand<'a>

§

impl<'a> !Unpin for Demand<'a>

§

impl<'a> !UnwindSafe for Demand<'a>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more