Rocket

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 64ba2056ef3678152646483c28a20231ad7ae98d
parent 32e2ff4d6b96292953e4b0b0056242046bcd89a0
Author: messense <messense@icloud.com>
Date:   Tue,  6 Nov 2018 10:59:44 +0800

Use 'RwLock' to make 'Client' 'Sync'.

Resolves #814.

Diffstat:
Mcore/lib/src/local/client.rs | 18+++++++++++++++---
Mcore/lib/src/local/request.rs | 5+++--
2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/core/lib/src/local/client.rs b/core/lib/src/local/client.rs @@ -1,4 +1,4 @@ -use std::cell::RefCell; +use std::sync::RwLock; use std::borrow::Cow; use Rocket; @@ -53,7 +53,7 @@ use error::LaunchError; /// [`post`]: #method.post pub struct Client { rocket: Rocket, - crate cookies: Option<RefCell<CookieJar>>, + crate cookies: Option<RwLock<CookieJar>>, } impl Client { @@ -62,7 +62,7 @@ impl Client { /// set to `None`. fn _new(rocket: Rocket, tracked: bool) -> Result<Client, LaunchError> { let cookies = match tracked { - true => Some(RefCell::new(CookieJar::new())), + true => Some(RwLock::new(CookieJar::new())), false => None }; @@ -321,3 +321,15 @@ impl Client { LocalRequest::new(self, method, uri.into()) } } + +#[cfg(test)] +mod test { + use super::Client; + + fn assert_sync<T: Sync>() {} + + #[test] + fn test_local_client_impl_sync() { + assert_sync::<Client>(); + } +} diff --git a/core/lib/src/local/request.rs b/core/lib/src/local/request.rs @@ -111,7 +111,8 @@ impl<'c> LocalRequest<'c> { // Set up any cookies we know about. if let Some(ref jar) = client.cookies { - for cookie in jar.borrow().iter() { + let cookies = jar.read().expect("LocalRequest::new() read lock"); + for cookie in cookies.iter() { request.cookies().add_original(cookie.clone().into_owned()); } } @@ -408,7 +409,7 @@ impl<'c> LocalRequest<'c> { // If the client is tracking cookies, updates the internal cookie jar // with the changes reflected by `response`. if let Some(ref jar) = client.cookies { - let mut jar = jar.borrow_mut(); + let mut jar = jar.write().expect("LocalRequest::_dispatch() write lock"); let current_time = ::time::now(); for cookie in response.cookies() { if let Some(expires) = cookie.expires() {