mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-03 01:34:32 +02:00
revert DateServiceInner changes
This commit is contained in:
@ -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<DateServiceInner>);
|
||||
|
||||
struct DateServiceInner {
|
||||
current: Cell<Option<(Date, Instant)>>,
|
||||
current: UnsafeCell<Option<(Date, Instant)>>,
|
||||
}
|
||||
|
||||
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<F: FnMut(&Date)>(&self, mut f: F) {
|
||||
self.check_date();
|
||||
|
||||
self.0.get().unwrap().0
|
||||
f(&unsafe { (&*self.0.current.get()).as_ref().unwrap().0 })
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user