ostr

Crates.ioostr
lib.rsostr
version
sourcesrc
created_at2024-12-08 18:18:28.523024
updated_at2024-12-09 08:46:29.143649
descriptionOwned str
homepage
repositoryhttps://github.com/vbogretsov/ostr
max_upload_size
id1476532
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`
size0
Vladimir Bogretsov (vbogretsov)

documentation

https://docs.rs/ostr

README

ostr

Owned const str

Rationale

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.

Commit count: 4

cargo fmt