Closed mimoo closed 1 year ago
I don't think it is. There are two problems here:
First, I belive the code, as written, is incorrect: fn digest(&self)
takes a self
as an argument (so, it might be called for different instances of the type), but you are trying to use a global cache here. These feels wrong, as the cache will be shared by unrelated things. You probably want something like this (note the absence of self):
trait Digest {
static CACHE: OnceCell<Shap256>;
fn digest() -> [u32; 32]
}
Sadly, that won't work either -- associated statics are not supported by a language. This has to do with monomorphisation -- for functions, separate compilation units get separate (duplicate) monomorphisation instances. This works, as functions don't have identity. statics, on the other hand, have to be unique, and this is not something currently officially suppoted by the Rust linking model.
So, I think no matter how you spin this, static CACHE: OnceCell<Sha256>
needs to be written by the user at the place where they declare an impl. Though, you might make this more automated by hiding this inside a macro.
Closing, as I don't think we should do anything in this crate
I wanted to implement an evaluate-once cache on a trait, so I stored a OnceCell in an associated const:
Cargo clippy returns the following error: https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
I can't seem to replace the
const
with astatic
I'm getting:
Any idea if this is at all possible :)?