fuchsia_inspect/writer/types/
property.rs1use crate::writer::private::InspectTypeInternal;
6use crate::writer::{Error, InnerType, State};
7use inspect_format::BlockIndex;
8
9pub trait Property<'t>: InspectTypeInternal {
11    type Type;
13
14    fn set(&self, value: Self::Type);
16
17    fn atomic_update<R, F: FnOnce(&Self) -> R>(&self, update_fn: F) -> R {
20        self.atomic_access(update_fn)
21    }
22}
23
24pub trait NumericProperty<'t>: Property<'t> {
26    fn add(&self, value: <Self as Property<'t>>::Type) -> Option<<Self as Property<'t>>::Type>;
28
29    fn subtract(&self, value: <Self as Property<'t>>::Type)
31    -> Option<<Self as Property<'t>>::Type>;
32}
33
34pub trait Length {
36    fn len(&self) -> Option<usize>;
37    fn is_empty(&self) -> Option<bool> {
38        self.len().map(|s| s == 0)
39    }
40}
41
42impl<T: ArrayProperty + InspectTypeInternal> Length for T {
43    fn len(&self) -> Option<usize> {
44        if let Ok(state) = self.state()?.try_lock() {
45            return Some(state.get_array_size(self.block_index()?));
46        }
47        None
48    }
49}
50
51pub trait ArrayProperty: Length + InspectTypeInternal {
53    type Type<'a>
55    where
56        Self: 'a;
57
58    fn set<'a>(&self, index: usize, value: impl Into<Self::Type<'a>>)
60    where
61        Self: 'a;
62
63    fn clear(&self);
65
66    fn atomic_update<R, F: FnOnce(&Self) -> R>(&self, update_fn: F) -> R {
69        self.atomic_access(update_fn)
70    }
71}
72
73pub trait ArithmeticArrayProperty: ArrayProperty {
74    fn add<'a>(&self, index: usize, value: Self::Type<'a>)
76    where
77        Self: 'a;
78
79    fn subtract<'a>(&self, index: usize, value: Self::Type<'a>)
81    where
82        Self: 'a;
83}
84
85pub trait HistogramProperty {
87    type Type;
89
90    fn insert(&self, value: Self::Type);
92
93    fn insert_multiple(&self, value: Self::Type, count: usize);
95
96    fn clear(&self);
98}
99
100#[derive(Default, Debug)]
101pub(crate) struct InnerPropertyType;
102
103impl InnerType for InnerPropertyType {
104    type Data = ();
105    fn free(state: &State, _: &Self::Data, block_index: BlockIndex) -> Result<(), Error> {
106        let mut state_lock = state.try_lock()?;
107        state_lock
108            .free_string_or_bytes_buffer_property(block_index)
109            .map_err(|err| Error::free("property", block_index, err))
110    }
111}