java_asm/
computable.rs

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
use crate::AsmResult;
use std::cell::UnsafeCell;
use std::rc::Rc;

#[derive(Clone, Debug)]
pub enum InnerValue<V> {
    UnInit,
    Initialized(V),
}


#[derive(Debug)]
pub struct Computable<V> {
    pub(crate) inner_value: UnsafeCell<InnerValue<Rc<V>>>,
}

pub trait ComputableOwner<V> {
    fn computable_ref(&self) -> &Computable<V>;
    fn compute(&self) -> AsmResult<V>;
}


pub trait ComputableAccessor<V> {
    fn force(&self) -> AsmResult<Rc<V>>;
}

pub struct ComputableSizedVec<V> {
    pub(crate) vec_ref: Vec<Computable<V>>,
}

pub trait ComputableSizedVecOwner<V> {
    fn computable_vec(&self) -> &ComputableSizedVec<V>;
    fn compute(&self, index: usize) -> AsmResult<V>;
}

pub trait ComputableSizedVecAccessor<V> {
    /// Get the value at the index, compute value if needed.
    /// Returns [AsmErr::OutOfRange] if the `index` is out of range. 
    fn get_or_compute(&self, index: usize) -> AsmResult<Rc<V>>;
}