From 407df9494536d0d6a297150c25fd44dc46a32fe7 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 17 Apr 2020 23:30:01 +0200 Subject: [PATCH] Switch to parking_lot::Mutex --- Cargo.lock | 31 +++++++++++++++++++++++++++++++ emigui/Cargo.toml | 2 +- emigui/src/context.rs | 26 ++++++++++++-------------- emigui/src/emigui.rs | 2 +- emigui/src/font.rs | 5 +++-- emigui/src/fonts.rs | 6 ++++-- emigui/src/layout.rs | 7 +++---- emigui/src/region.rs | 16 +++------------- emigui/src/window.rs | 11 +++++------ 9 files changed, 63 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7b880d82..149f5684 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,6 +154,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "emigui" version = "0.1.0" dependencies = [ + "parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", "rusttype 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", @@ -455,6 +456,15 @@ dependencies = [ "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot_core 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "parking_lot_core" version = "0.6.2" @@ -469,6 +479,19 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot_core" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "percent-encoding" version = "2.1.0" @@ -628,6 +651,11 @@ dependencies = [ "maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "smallvec" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "smithay-client-toolkit" version = "0.4.6" @@ -921,8 +949,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" "checksum ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "18869315e81473c951eb56ad5558bbc56978562d3ecfb87abb7a1e944cea4518" "checksum osmesa-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "88cfece6e95d2e717e0872a7f53a8684712ad13822a7979bc760b9c77ec0013b" +"checksum parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" "checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" "checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" +"checksum parking_lot_core 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0e136c1904604defe99ce5fd71a28d473fa60a12255d511aa78a9ddf11237aeb" "checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" @@ -945,6 +975,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_json 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)" = "da07b57ee2623368351e9a0488bb0b261322a15a6e0ae53e243cbdc0f4208da9" "checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" "checksum smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6" +"checksum smallvec 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05720e22615919e4734f6a99ceae50d00226c3c5aca406e102ebc33298214e0a" "checksum smithay-client-toolkit 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2ccb8c57049b2a34d2cc2b203fa785020ba0129d31920ef0d317430adaf748fa" "checksum stb_truetype 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f77b6b07e862c66a9f3e62a07588fee67cd90a9135a2b942409f195507b4fb51" "checksum syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03" diff --git a/emigui/Cargo.toml b/emigui/Cargo.toml index edd8385b..2dc930a4 100644 --- a/emigui/Cargo.toml +++ b/emigui/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [lib] [dependencies] -# palette = "0.4" +parking_lot = "0.10" rusttype = "0.8" serde = "1" serde_derive = "1" diff --git a/emigui/src/context.rs b/emigui/src/context.rs index 561abf46..37c9c84c 100644 --- a/emigui/src/context.rs +++ b/emigui/src/context.rs @@ -1,4 +1,6 @@ -use std::sync::{Arc, Mutex}; +use std::sync::Arc; + +use parking_lot::Mutex; use crate::*; @@ -18,8 +20,8 @@ impl Clone for Context { style: Mutex::new(self.style()), fonts: self.fonts.clone(), input: self.input, - memory: Mutex::new(self.memory.lock().unwrap().clone()), - graphics: Mutex::new(self.graphics.lock().unwrap().clone()), + memory: Mutex::new(self.memory.lock().clone()), + graphics: Mutex::new(self.graphics.lock().clone()), } } } @@ -40,37 +42,33 @@ impl Context { } pub fn style(&self) -> Style { - *self.style.lock().unwrap() + *self.style.lock() } pub fn set_style(&self, style: Style) { - *self.style.lock().unwrap() = style; + *self.style.lock() = style; } // TODO: move pub fn new_frame(&mut self, gui_input: GuiInput) { self.input = gui_input; if !gui_input.mouse_down || gui_input.mouse_pos.is_none() { - self.memory.lock().unwrap().active_id = None; + self.memory.lock().active_id = None; } } pub fn drain_paint_lists(&self) -> Vec { - let memory = self.memory.lock().unwrap(); - self.graphics - .lock() - .unwrap() - .drain(&memory.window_order) - .collect() + let memory = self.memory.lock(); + self.graphics.lock().drain(&memory.window_order).collect() } /// Is the user interacting with anything? pub fn any_active(&self) -> bool { - self.memory.lock().unwrap().active_id.is_some() + self.memory.lock().active_id.is_some() } pub fn interact(&self, layer: Layer, rect: Rect, interaction_id: Option) -> InteractInfo { - let mut memory = self.memory.lock().unwrap(); + let mut memory = self.memory.lock(); let hovered = if let Some(mouse_pos) = self.input.mouse_pos { if rect.contains(mouse_pos) { diff --git a/emigui/src/emigui.rs b/emigui/src/emigui.rs index 175896a0..318b10ae 100644 --- a/emigui/src/emigui.rs +++ b/emigui/src/emigui.rs @@ -55,7 +55,7 @@ impl Emigui { } pub fn paint(&mut self) -> Mesh { - let paint_commands: Vec = self.ctx.graphics.lock().unwrap().drain().collect(); + let paint_commands = self.ctx.drain_paint_lists(); let mut mesher = Mesher::new(self.last_input.pixels_per_point); mesher.anti_alias = self.anti_alias; diff --git a/emigui/src/font.rs b/emigui/src/font.rs index ace7b147..05ef7aa0 100644 --- a/emigui/src/font.rs +++ b/emigui/src/font.rs @@ -1,5 +1,6 @@ -use std::sync::{Arc, Mutex}; +use std::sync::Arc; +use parking_lot::Mutex; use rusttype::{point, Scale}; use crate::{ @@ -96,7 +97,7 @@ impl Font { .collect(); let mut glyph_infos = vec![]; - let mut atlas_lock = atlas.lock().unwrap(); + let mut atlas_lock = atlas.lock(); for glyph in glyphs { let uv = if let Some(bb) = glyph.pixel_bounding_box() { diff --git a/emigui/src/fonts.rs b/emigui/src/fonts.rs index 14435533..f96878c2 100644 --- a/emigui/src/fonts.rs +++ b/emigui/src/fonts.rs @@ -1,9 +1,11 @@ use std::{ collections::{hash_map::DefaultHasher, BTreeMap}, hash::{Hash, Hasher}, - sync::{Arc, Mutex}, + sync::Arc, }; +use parking_lot::Mutex; + use crate::{ font::Font, texture_atlas::{Texture, TextureAtlas}, @@ -102,7 +104,7 @@ impl Fonts { ) }) .collect(); - self.texture = atlas.lock().unwrap().texture().clone(); + self.texture = atlas.lock().texture().clone(); let mut hasher = DefaultHasher::new(); self.texture.pixels.hash(&mut hasher); diff --git a/emigui/src/layout.rs b/emigui/src/layout.rs index 696fc4f9..bc56908b 100644 --- a/emigui/src/layout.rs +++ b/emigui/src/layout.rs @@ -98,7 +98,7 @@ where F: FnOnce(&mut Region), { let layer = Layer::Popup; - let where_to_put_background = ctx.graphics.lock().unwrap().layer(layer).len(); + let where_to_put_background = ctx.graphics.lock().layer(layer).len(); let style = ctx.style(); let window_padding = style.window_padding; @@ -125,9 +125,8 @@ where let rect = Rect::from_min_size(window_pos, outer_size); - let mut graphics = ctx.graphics.lock().unwrap(); - let graphics = graphics.layer(layer); - graphics.insert( + let mut graphics = ctx.graphics.lock(); + graphics.layer(layer).insert( where_to_put_background, PaintCmd::Rect { corner_radius: 5.0, diff --git a/emigui/src/region.rs b/emigui/src/region.rs index 489e1f13..f4765c81 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -39,21 +39,11 @@ impl Region { /// Can be used for free painting. /// NOTE: all coordinates are screen coordinates! pub fn add_paint_cmd(&mut self, paint_cmd: PaintCmd) { - self.ctx - .graphics - .lock() - .unwrap() - .layer(self.layer) - .push(paint_cmd) + self.ctx.graphics.lock().layer(self.layer).push(paint_cmd) } pub fn add_paint_cmds(&mut self, mut cmds: Vec) { - self.ctx - .graphics - .lock() - .unwrap() - .layer(self.layer) - .append(&mut cmds) + self.ctx.graphics.lock().layer(self.layer).append(&mut cmds) } /// Options for this region, and any child regions we may spawn. @@ -124,7 +114,7 @@ impl Region { ); let open = { - let mut memory = self.ctx.memory.lock().unwrap(); + let mut memory = self.ctx.memory.lock(); if interact.clicked { if memory.open_foldables.contains(&id) { memory.open_foldables.remove(&id); diff --git a/emigui/src/window.rs b/emigui/src/window.rs index 4fc46add..0f1488c7 100644 --- a/emigui/src/window.rs +++ b/emigui/src/window.rs @@ -30,7 +30,7 @@ impl Window { { let id = make_id(&self.title); - let mut state = ctx.memory.lock().unwrap().get_or_create_window( + let mut state = ctx.memory.lock().get_or_create_window( id, Rect::from_min_size( vec2(400.0, 200.0), // TODO @@ -39,7 +39,7 @@ impl Window { ); let layer = Layer::Window(id); - let where_to_put_background = ctx.graphics.lock().unwrap().layer(layer).len(); + let where_to_put_background = ctx.graphics.lock().layer(layer).len(); let style = ctx.style(); let window_padding = style.window_padding; @@ -69,9 +69,8 @@ impl Window { state.rect = Rect::from_min_size(state.rect.min(), outer_size); - let mut graphics = ctx.graphics.lock().unwrap(); - let graphics = graphics.layer(layer); - graphics.insert( + let mut graphics = ctx.graphics.lock(); + graphics.layer(layer).insert( where_to_put_background, PaintCmd::Rect { corner_radius: 5.0, @@ -89,7 +88,7 @@ impl Window { state.rect = state.rect.translate(ctx.input().mouse_move); } - let mut memory = ctx.memory.lock().unwrap(); + let mut memory = ctx.memory.lock(); if interact.active || interact.clicked { memory.move_window_to_top(id); }