| Crates.io | ostr |
| lib.rs | ostr |
| version | 0.1.1 |
| created_at | 2024-12-08 18:18:28.523024+00 |
| updated_at | 2024-12-09 08:46:29.143649+00 |
| description | Owned str |
| homepage | |
| repository | https://github.com/vbogretsov/ostr |
| max_upload_size | |
| id | 1476532 |
| size | 13,754 |
Owned const str
This library provides owned str instance. It can be used in complex keys
in HashMap or HashSet without leveraging unstable raw entry API.
Example:
use ostr::Str;
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct SchemaKey {
subject: Str,
version: i32,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct SchemaKeyRef<'a> {
subject: &'a str,
version: i32,
}
impl<'a> Borrow<SchemaKeyRef<'a>> for SchemaKey {
fn borrow(&self) -> &SchemaKeyRef<'a> {
unsafe {
&*(self as *const SchemaKey as *const SchemaKeyRef)
}
}
}
fn main() {
let mut cache: HashMap<SchemaKey, String> = HashMap::new();
cache.insert(
SchemaKey{subject: Str::new("User"), version: 1},
"User:1".to_string(),
);
cache.insert(
SchemaKey{subject: Str::new("User"), version: 2},
"User:2".to_string(),
);
let key = SchemaKeyRef{subject: "User", version: 1};
assert_eq!(cache.get(&key), Some(&"User:1".to_string()));
}
It is guarantied size of Str to be equal to size of &str.