free-librespot/capi/src/cstring_cache.rs
2016-02-05 21:11:55 +00:00

21 lines
437 B
Rust

use std::collections::HashMap;
use std::ffi::{CString, CStr};
pub struct CStringCache {
cache: HashMap<String, CString>
}
impl CStringCache {
pub fn new() -> CStringCache {
CStringCache {
cache: HashMap::new()
}
}
pub fn intern(&mut self, string: &str) -> &CStr {
self.cache.entry(string.to_owned()).or_insert_with(|| {
CString::new(string).unwrap()
})
}
}