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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::fmt::Debug;
use std::sync::Arc;
use crate::Realm;
use crate::array::RealmRef;
/// Trait for nodes in the realm. A node is a struct that can be created from a
/// reference to its realm and reference.
pub(crate) trait Node {
fn from_ref(realm: Arc<Realm>, ref_: RealmRef) -> crate::RealmResult<Self>
where
Self: Sized;
}
/// Blanket implementation: any node with an empty context implements Node
impl<T> Node for T
where
T: NodeWithContext<()>,
{
fn from_ref(realm: Arc<Realm>, ref_: RealmRef) -> crate::RealmResult<Self>
where
Self: Sized,
{
Self::from_ref_with_context(realm, ref_, ())
}
}
/// Trait for nodes in the realm, holding a context. A node is a struct that can
/// be created from a reference to its realm and reference.
pub(crate) trait NodeWithContext<T> {
fn from_ref_with_context(
realm: Arc<Realm>,
ref_: RealmRef,
context: T,
) -> crate::RealmResult<Self>
where
Self: Sized;
}
/// Array-like trait for nodes in the realm, holding a context. These allow
/// fetching elements of the given type from the array.
pub(crate) trait ArrayLike<T, Context = ()>: NodeWithContext<Context> + Debug {
/// Get the value at the given index.
fn get(&self, index: usize) -> crate::RealmResult<T>;
/// Get the value at the given index directly from the realm, without
/// allocating an instance of the array.
fn get_direct(
realm: Arc<Realm>,
ref_: RealmRef,
index: usize,
context: Context,
) -> crate::RealmResult<T>
where
Self: Sized;
/// Get all values from the array.
fn get_all(&self) -> crate::RealmResult<Vec<T>> {
(0..self.size()).map(|i| self.get(i)).collect()
}
/// Check if the value at the given index is null.
fn is_null(&self, index: usize) -> crate::RealmResult<bool>;
/// Get the size of the array, indicating the number of elements it contains.
fn size(&self) -> usize;
}