From 9c3789cbd0c607d58f024afd2e51dc2f6a1198d5 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 18 Jul 2019 17:37:41 +0600 Subject: [PATCH] revert DateServiceInner changes --- actix-http/CHANGES.md | 7 +++++++ actix-http/src/config.rs | 31 +++++++++++++++---------------- actix-http/src/error.rs | 7 +++++-- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 84033531d..cc695fa63 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -1,5 +1,12 @@ # Changes +## [0.2.7] - 2019-07-18 + +### Added + +* Add support for downcasting response errors #986 + + ## [0.2.6] - 2019-07-17 ### Changed diff --git a/actix-http/src/config.rs b/actix-http/src/config.rs index 7d0e27635..bdfecef30 100644 --- a/actix-http/src/config.rs +++ b/actix-http/src/config.rs @@ -1,4 +1,4 @@ -use std::cell::Cell; +use std::cell::UnsafeCell; use std::fmt; use std::fmt::Write; use std::rc::Rc; @@ -162,13 +162,17 @@ impl ServiceConfig { pub fn set_date(&self, dst: &mut BytesMut) { let mut buf: [u8; 39] = [0; 39]; buf[..6].copy_from_slice(b"date: "); - buf[6..35].copy_from_slice(&self.0.timer.date().bytes); + self.0 + .timer + .set_date(|date| buf[6..35].copy_from_slice(&date.bytes)); buf[35..].copy_from_slice(b"\r\n\r\n"); dst.extend_from_slice(&buf); } pub(crate) fn set_date_header(&self, dst: &mut BytesMut) { - dst.extend_from_slice(&self.0.timer.date().bytes); + self.0 + .timer + .set_date(|date| dst.extend_from_slice(&date.bytes)); } } @@ -206,28 +210,24 @@ impl fmt::Write for Date { struct DateService(Rc); struct DateServiceInner { - current: Cell>, + current: UnsafeCell>, } impl DateServiceInner { fn new() -> Self { DateServiceInner { - current: Cell::new(None), + current: UnsafeCell::new(None), } } - fn get(&self) -> Option<(Date, Instant)> { - self.current.get() - } - fn reset(&self) { - self.current.set(None); + unsafe { (&mut *self.current.get()).take() }; } fn update(&self) { let now = Instant::now(); let date = Date::new(); - self.current.set(Some((date, now))); + *(unsafe { &mut *self.current.get() }) = Some((date, now)); } } @@ -237,7 +237,7 @@ impl DateService { } fn check_date(&self) { - if self.0.get().is_none() { + if unsafe { (&*self.0.current.get()).is_none() } { self.0.update(); // periodic date update @@ -253,13 +253,12 @@ impl DateService { fn now(&self) -> Instant { self.check_date(); - self.0.get().unwrap().1 + unsafe { (&*self.0.current.get()).as_ref().unwrap().1 } } - fn date(&self) -> Date { + fn set_date(&self, mut f: F) { self.check_date(); - - self.0.get().unwrap().0 + f(&unsafe { (&*self.0.current.get()).as_ref().unwrap().0 }) } } diff --git a/actix-http/src/error.rs b/actix-http/src/error.rs index c4d663ade..cbb009a72 100644 --- a/actix-http/src/error.rs +++ b/actix-http/src/error.rs @@ -1,10 +1,10 @@ //! Error and Result module +use std::any::TypeId; use std::cell::RefCell; use std::io::Write; use std::str::Utf8Error; use std::string::FromUtf8Error; use std::{fmt, io, result}; -use std::any::TypeId; pub use actix_threadpool::BlockingError; use actix_utils::timeout::TimeoutError; @@ -81,7 +81,10 @@ pub trait ResponseError: fmt::Debug + fmt::Display { } #[doc(hidden)] - fn __private_get_type_id__(&self) -> TypeId where Self: 'static { + fn __private_get_type_id__(&self) -> TypeId + where + Self: 'static, + { TypeId::of::() } }