Indeed, Rust does not support generic statics, for technical reasons. That being said, do you really need the value to be explicitly stored in one singlestatic storage?
If you only want the value to be precomputed at compile time, you can use a const YOUR_VALUE: Ty = …;.
While this is not generic on its own, you can use a helper trait to emulate that:
- trait Object<T> {
+ trait Object<T> : HasGType<T> {
fn init(&self) -> T;
fn ret(&self) -> T {
- static GTYPE: GetType<T> = GetType::<T>(None); // Error: Can't use generic parameters from outer function.
- let get_type = GetType::<T>(None); // It work!
+ let get_type = <Self as HasGType<T>>::GTYPE;
self.init()
}
}
+ trait HasGType<T> {
+ const GTYPE: GetType<T> = GetType::<T>(None);
+ }
+ impl<T, U : ?Sized> HasGType<T> for U {}
If you also need a certain amount of static storage, although some amount of duplication could be acceptable, then you can simply try to have a generic constant, as above, but whose type is prefixed with &'static:
const GTYPE: &'static GetType<T>;
You'll need to add a T : 'static bound for this to work without infecting everything with lifetimes.
And once you have T : 'static, if you are willing to pay a little bit of runtime lookup cost in order to achieve full static storage unicity, then you can use: