ferroui_dynamic_data/binding/
property_value.rs1use std::fmt;
4
5#[derive(Clone)]
10pub struct PropertyValue<T, V> {
11 sender: T,
12 value: Option<V>,
13}
14
15impl<T, V> PropertyValue<T, V> {
16 pub fn new(sender: T, value: V) -> Self {
18 Self { sender, value: Some(value) }
19 }
20
21 pub fn unobtainable(sender: T) -> Self {
24 Self { sender, value: None }
25 }
26
27 pub fn sender(&self) -> &T {
29 &self.sender
30 }
31
32 pub fn value(&self) -> Option<&V> {
34 self.value.as_ref()
35 }
36
37 pub fn into_value(self) -> Option<V> {
39 self.value
40 }
41
42 pub fn is_unobtainable(&self) -> bool {
44 self.value.is_none()
45 }
46}
47
48impl<T: PartialEq, V: PartialEq> PartialEq for PropertyValue<T, V> {
49 fn eq(&self, other: &Self) -> bool {
52 match (&self.value, &other.value) {
53 (None, None) => true,
54 (Some(value), Some(other_value)) => self.sender == other.sender && value == other_value,
55 _ => false,
56 }
57 }
58}
59
60impl<T: fmt::Display, V: fmt::Debug> fmt::Display for PropertyValue<T, V> {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 match &self.value {
64 Some(value) => write!(f, "{} ({:?})", self.sender, value),
65 None => write!(f, "{} ()", self.sender),
66 }
67 }
68}
69
70impl<T: fmt::Debug, V: fmt::Debug> fmt::Debug for PropertyValue<T, V> {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 f.debug_struct("PropertyValue").field("sender", &self.sender).field("value", &self.value).finish()
73 }
74}