From: Ian Jackson Date: Fri, 27 Nov 2020 23:53:18 +0000 (+0000) Subject: shapelib: Use parking_lot, not rental X-Git-Tag: otter-0.2.0~321 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=94d03bbd74a2fb2a447df471a53cd4600c2ac48f;p=otter.git shapelib: Use parking_lot, not rental Signed-off-by: Ian Jackson --- diff --git a/src/global.rs b/src/global.rs index 76f452bd..727ca872 100644 --- a/src/global.rs +++ b/src/global.rs @@ -192,8 +192,6 @@ pub struct Global { // per-game lock: // <- InstanceContainer -> - pub shapelibs : RwLock, - // inner locks which the game needs: dirty : Mutex>, pub config: RwLock>, diff --git a/src/shapelib.rs b/src/shapelib.rs index 3cd0e66e..76cc8014 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -4,6 +4,8 @@ pub use crate::imports::*; +use parking_lot::{RwLock, const_rwlock, RwLockReadGuard, MappedRwLockReadGuard}; + // Naming convention: // *Data, *List from toml etc. (processed if need be) // *Defn raw read from library toml file (where different from Info) @@ -191,14 +193,19 @@ impl Piece for Item { } +static SHAPELIBS : RwLock> = const_rwlock(None); + #[throws(SpecError)] pub fn libs_lookup(libname: &str) - -> RentRef, - Contents> { - let libs = GLOBAL.shapelibs.read().unwrap(); - RentRef::try_new(libs,|libs| Ok::<_,SpecError>({ - libs.get(libname).ok_or(SE::LibraryNotFound)? - })).map_err(|e|e.0)? + -> MappedRwLockReadGuard<'static, Contents> { + let libs = SHAPELIBS.read(); + RwLockReadGuard::try_map( libs, |libs: &Option| -> Option<_> { + (|| Some({ + libs.as_ref()?.get(libname)? + }))() + }) + .map_err(|_| SE::LibraryNotFound) + ? } impl ItemSpec { @@ -398,7 +405,9 @@ pub struct Explicit1 { pub fn load1(l: &Explicit1) { let data = load_catalogue(&l.name, &l.dirname, &l.catalogue)?; let count = data.items.len(); - GLOBAL.shapelibs.write().unwrap().insert(l.name.clone(), data); + SHAPELIBS.write() + .get_or_insert_with(default) + .insert(l.name.clone(), data); info!("loaded {} shapes in library {:?} from {:?} and {:?}", count, &l.name, &l.catalogue, &l.dirname); }