diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-09-03 12:08:11 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-09-03 12:36:04 +0200 |
commit | 7901c7ee84e7863c5227ded5f29a36beddbcecb8 (patch) | |
tree | 353d8602323cabb53a9b3b51892b7686f8a51048 /openpgp | |
parent | 7dac87c8d4d9a6ef7fe069cf78a640b4d57f6a28 (diff) |
openpgp: Make UserID Sync.
- Serialize access to the cache using a sync::Mutex.
- See #334.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/packet/userid.rs | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/openpgp/src/packet/userid.rs b/openpgp/src/packet/userid.rs index 1a3a05ce..eb70d033 100644 --- a/openpgp/src/packet/userid.rs +++ b/openpgp/src/packet/userid.rs @@ -3,6 +3,7 @@ use std::str; use std::hash::{Hash, Hasher}; use std::cell::RefCell; use std::cmp::Ordering; +use std::sync::Mutex; use quickcheck::{Arbitrary, Gen}; use crate::rfc2822::{ @@ -50,7 +51,7 @@ pub struct UserID { /// Use `UserID::default()` to get a UserID with a default settings. value: Vec<u8>, - parsed: RefCell<Option<ParsedUserID>>, + parsed: Mutex<RefCell<Option<ParsedUserID>>>, } impl From<Vec<u8>> for UserID { @@ -58,7 +59,7 @@ impl From<Vec<u8>> for UserID { UserID { common: Default::default(), value: u, - parsed: RefCell::new(None), + parsed: Mutex::new(RefCell::new(None)), } } } @@ -294,10 +295,11 @@ impl UserID { } fn do_parse(&self) -> Result<()> { - if self.parsed.borrow().is_none() { + if self.parsed.lock().unwrap().borrow().is_none() { let s = str::from_utf8(&self.value)?; - *self.parsed.borrow_mut() = Some(match NameAddrOrOther::parse(s) { + *self.parsed.lock().unwrap().borrow_mut() = + Some(match NameAddrOrOther::parse(s) { Ok(na) => ParsedUserID { name: na.name().map(|s| s.to_string()), comment: na.comment().map(|s| s.to_string()), @@ -332,7 +334,7 @@ impl UserID { /// input is okay, this still returns the display name. pub fn name(&self) -> Result<Option<String>> { self.do_parse()?; - match *self.parsed.borrow() { + match *self.parsed.lock().unwrap().borrow() { Some(ParsedUserID { ref name, .. }) => Ok(name.as_ref().map(|s| s.clone())), None => unreachable!(), @@ -346,7 +348,7 @@ impl UserID { /// input is okay, this still returns the first comment. pub fn comment(&self) -> Result<Option<String>> { self.do_parse()?; - match *self.parsed.borrow() { + match *self.parsed.lock().unwrap().borrow() { Some(ParsedUserID { ref comment, .. }) => Ok(comment.as_ref().map(|s| s.clone())), None => unreachable!(), @@ -361,7 +363,7 @@ impl UserID { /// `UserID::other_address()`. pub fn address(&self) -> Result<Option<String>> { self.do_parse()?; - match *self.parsed.borrow() { + match *self.parsed.lock().unwrap().borrow() { Some(ParsedUserID { address: Ok(ref address), .. }) => Ok(Some(address.clone())), Some(ParsedUserID { address: Err(_), .. }) => @@ -387,7 +389,7 @@ impl UserID { /// invalid address can be obtained using `NameAddrOrOther::other()`. pub fn other(&self) -> Result<Option<String>> { self.do_parse()?; - match *self.parsed.borrow() { + match *self.parsed.lock().unwrap().borrow() { Some(ParsedUserID { ref other, .. }) => Ok(other.as_ref().map(|s| s.clone())), None => unreachable!(), @@ -407,7 +409,7 @@ impl UserID { /// `ssh://server.example.net`. pub fn other_or_address(&self) -> Result<Option<String>> { self.do_parse()?; - match *self.parsed.borrow() { + match *self.parsed.lock().unwrap().borrow() { Some(ParsedUserID { address: Ok(ref address), .. }) => Ok(Some(address.clone())), Some(ParsedUserID { ref other, .. }) => |