#storage #generic #type #owned #traits #alloc #helper

no-std stor

Helpers for genericising over storage types

2 releases

0.1.1 Mar 12, 2022
0.1.0 Jan 15, 2022

#641 in Memory management

MPL-2.0 license

4KB

Stor trait defines underlying storage for use in type definitions

use stor::Stor;

/// Generic object over storage types
enum Something<S: Stor<()>> {
  List(S::List),
  Str(S::String),
  Bytes(S::Bytes),
}

/// Owned version (requires `alloc` feature)
type SomethingOwned = Something<stor::Owned>;
let _ = SomethingOwned::Str("hello".to_string());
let _ = SomethingOwned::Bytes(vec![0xaa, 0xbb, 0xcc]);

/// Reference version
type SomethingRef<'a> = Something<stor::Ref<'a>>;
let _ = SomethingRef::Str("hello");
let _ = SomethingRef::Bytes(&[0xaa, 0xbb, 0xcc]);

/// Const N version
type SomethingConst<'a> = Something<stor::Const<3>>;
let _ = SomethingConst::Str("hello");
let _ = SomethingConst::Bytes([0xaa, 0xbb, 0xcc]);

Dependencies

~635KB
~12K SLoC