cw_storage_plus/
namespace.rs

1use std::borrow::Cow;
2
3/// The namespace of a storage container. Meant to be constructed from "stringy" types.
4///
5/// This type is generally not meant to be constructed directly. It's exported for
6/// documentation purposes. Most of the time, you should just pass a [`String`] or
7/// `&'static str` to an [`Item`](crate::Item)/collection constructor.
8#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
9pub struct Namespace(Cow<'static, [u8]>);
10
11impl Namespace {
12    pub const fn from_static_str(s: &'static str) -> Namespace {
13        Namespace(Cow::Borrowed(s.as_bytes()))
14    }
15
16    pub fn as_slice(&self) -> &[u8] {
17        self.0.as_ref()
18    }
19}
20
21impl From<&'static str> for Namespace {
22    fn from(s: &'static str) -> Self {
23        Namespace(Cow::Borrowed(s.as_bytes()))
24    }
25}
26
27impl From<String> for Namespace {
28    fn from(s: String) -> Self {
29        Namespace(Cow::Owned(s.into_bytes()))
30    }
31}
32
33impl From<Cow<'static, [u8]>> for Namespace {
34    fn from(s: Cow<'static, [u8]>) -> Self {
35        Namespace(s)
36    }
37}