Crates.io | ostr |
lib.rs | ostr |
version | |
source | src |
created_at | 2024-12-08 18:18:28.523024 |
updated_at | 2024-12-09 08:46:29.143649 |
description | Owned str |
homepage | |
repository | https://github.com/vbogretsov/ostr |
max_upload_size | |
id | 1476532 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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
.