I have a library that uses Arc in defining, what is essentially an AST, I'm using a type
to
roughly something like:
pub type ShrPtr<T> = std::sync::Arc<T>;
pub struct GetClampAbove<T, B, Min> {
binding: ShrPtr<B>,
min: ShrPtr<Min>,
_phantom: PhantomData<fn() -> T>,
}
...
let input = .. get some input;
let min = ShrPtr::new(20);
let above = ShrPtr::new(GetClampAbove::new(input, min));
let mul = ShrPtr::new(GetMul::new(above, ShrPtr::new(20)));
I'm trying to make a no_std
compatible version of my library and I'd like to be able to build these AST like structures with entirely statically allocated data... no heap.
Arc::new
takes ownership of its argument, allocates space for it on the heap, copies it there, and then keeps track of it, so we know that the data lives as long as the Arc. I'm not sure how to mimic this 'take ownership' but then also be able to create clones that have access to the same data. I have an initial approach here: Rust Playground
This doesn't mimic Arc
exactly because it takes a reference in new
but I'm fine with modifying how I create the AST if I can use it in the same manner as with the dynamic, Arc
based approach. It doesn't work because to use it I have to modify anything that holds my Src
wrapper to indicate T: 'static
which would mean the stucts that hold these wouldn't be compatible with Arc
anymore.
I'm wondering if anyone has any advice for an approach?