summaryrefslogtreecommitdiffstats
path: root/src/tuice/state.rs
blob: 144c129f778e4f07e642f3b98d223f23cbb0eb58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//! State code is heavily inspired by crochet's work - see
//! [here](https://github.com/raphlinus/crochet/blob/master/src/state.rs) for the original.

use std::any::Any;

/// A trait that any sort of [`Component`](crate::tuice::Component) state should implement.
pub trait State {
    fn as_any(&self) -> &dyn Any;

    fn are_equal(&self, other: &dyn State) -> bool;
}

impl<S: PartialEq + 'static> State for S {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn are_equal(&self, other: &dyn State) -> bool {
        other
            .as_any()
            .downcast_ref()
            .map(|other| self == other)
            .unwrap_or(false)
    }
}