Documentation
  • Coverage
  • 20%
    1 out of 5 items documented1 out of 4 items with examples
  • Size
  • Source code size: 13.75 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 353.18 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 8s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • vbogretsov/ostr
    0 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • vbogretsov

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.