Trait UpdateInput

Source
pub trait UpdateInput:
    Named
    + HashValue
    + Clean {
    type Update;

    // Required method
    fn update_mut(&mut self, update: Self::Update);
}
Expand description

Describe how to update an InputNode value, and how that mutates the internal state. Correct implementation of this trait requires that any temporary state tracked is cleared up when implementing Clean.

§Example

It’s not uncommon for a node which wraps a (potentially large) collection to want to provide dependent nodes an accessor to know which of those have changed since this node was last resolved. Correct implementation of that pattern requires implementation of Clean.

pub struct MyNode {
    // Appends every item to this collection.
    all_things: Vec<i32>,
    // An index where the 'new' items start.
    new_things_index: usize,
}

impl UpdateInput for MyNode {
    type Update = i32;

    fn update_mut(&mut self, input: Self::Update) {
        self.all_things.push(input)
    }
}

impl MyNode {
    // Provide a convenience for dependees to iterate only the new things.
    pub fn just_the_new_things_thanks(&self) -> impl Iterator<Item = i32> + '_ {
        (self.new_things_index..).map_while(|idx| self.all_things.get(idx).copied())
    }
}

impl Clean for MyNode {
    fn clean(&mut self) {
        // Ensure nothing is yielded next time.
        self.new_things_index = self.all_things.len();
    }
}

let mut node = MyNode::default();

// Add some initial data
node.update_mut(5);

assert_eq!(
    node.just_the_new_things_thanks().collect::<Vec<_>>(),
    vec![5]
);

// After cleaning, any temporary state is flushed.
node.clean();
assert_eq!(
    node.just_the_new_things_thanks().collect::<Vec<_>>(),
    vec![]
);

// Only the latest values are shown.
node.update_mut(7);
node.update_mut(6);
assert_eq!(
    node.just_the_new_things_thanks().collect::<Vec<_>>(),
    vec![7, 6]
);

// Just to hammer home the point.
node.clean();
assert_eq!(
    node.just_the_new_things_thanks().collect::<Vec<_>>(),
    vec![]
);

In the example above, if Clean wasn’t implemented, the new values would be displayed to dependent nodes after this node had been cleaned. This is an unsound implementation, which violates the caching logic of depends.

Required Associated Types§

Required Methods§

Source

fn update_mut(&mut self, update: Self::Update)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl UpdateInput for bool

Source§

type Update = bool

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for char

Source§

type Update = char

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for i8

Source§

type Update = i8

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for i16

Source§

type Update = i16

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for i32

Source§

type Update = i32

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for i64

Source§

type Update = i64

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for i128

Source§

type Update = i128

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for isize

Source§

type Update = isize

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for u8

Source§

type Update = u8

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for u16

Source§

type Update = u16

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for u32

Source§

type Update = u32

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for u64

Source§

type Update = u64

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for u128

Source§

type Update = u128

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for usize

Source§

type Update = usize

Source§

fn update_mut(&mut self, update: Self::Update)

Source§

impl UpdateInput for String

Source§

type Update = String

Source§

fn update_mut(&mut self, update: Self::Update)

Implementors§