From e6cf4740c06d81c864d7fa6b9dc8d8753c3b3f88 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 16 Oct 2020 01:20:39 +0100 Subject: [PATCH] before enum for timezone Signed-off-by: Ian Jackson --- src/gamestate.rs | 5 +---- src/imports.rs | 2 ++ src/lib.rs | 1 + src/tz.rs | 43 ++++++++++++++++++++++++++++++------------- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/gamestate.rs b/src/gamestate.rs index 62f9df63..792b76cd 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -27,9 +27,6 @@ pub struct Html (pub String); #[serde(transparent)] pub struct Timestamp(pub u64); /* time_t */ -#[derive(Clone,Debug,Default,Serialize,Deserialize)] -pub struct Timezone { } // todo - pub const DEFAULT_TABLE_SIZE : Pos = PosC([ 400, 200 ]); // ---------- general data types ---------- @@ -175,7 +172,7 @@ impl Timestamp { pub fn render(&self, tz: &Timezone) -> String { let s = String::with_capacity(30); - tz.format(&mut s).unwrap(); + tz.format(*self, &mut s).unwrap(); s } } diff --git a/src/imports.rs b/src/imports.rs index 6c293f41..53b6f993 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -118,12 +118,14 @@ pub use crate::utils::*; pub use crate::spec::*; pub use crate::debugreader::DebugReader; pub use crate::shapelib; +pub use crate::tz::*; pub use zcoord::{self, ZCoord}; pub use nix::unistd::Uid; pub fn default() -> T { Default::default() } + pub enum Impossible { } pub type E = anyhow::Error; diff --git a/src/lib.rs b/src/lib.rs index 96163a0a..d095695a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // There is NO WARRANTY. +#![feature(never_type)] #![feature(proc_macro_hygiene, decl_macro)] #![feature(slice_strip)] diff --git a/src/tz.rs b/src/tz.rs index 9d75520e..81cbda55 100644 --- a/src/tz.rs +++ b/src/tz.rs @@ -8,16 +8,24 @@ use parking_lot::{RwLock, const_rwlock}; #[derive(SerializeDisplay)] #[derive(DeserializeFromStr)] +#[derive(Clone,Debug)] pub struct Timezone (Arc); -pub trait TimeFormatter : Debug { - fn format(&self, ts: Timestamp, f: &mut Formatter) -> fmt::Result<()>; +impl Timezone { + delegate! { to self.0 { + pub fn format(&self, ts: Timestamp, f: &mut Formatter) -> fmt::Result; + } } +} + +pub trait TimeFormatter : Debug + Sync + Send { + fn format(&self, ts: Timestamp, f: &mut Formatter) -> fmt::Result; fn name(&self) -> &str; } impl Display for Timezone { - fn fmt(&self, f: &mut Formatter) -> io::Result<()> { - write!(f, "{}", self.0.name()) + #[throws(fmt::Error)] + fn fmt(&self, f: &mut Formatter) { + write!(f, "{}", self.0.name())?; } } @@ -27,11 +35,11 @@ struct ChronoTz { ctz: TZ, } -impl TimeFormatter for ChronoTz { +impl TimeFormatter for ChronoTz { fn name(&self) -> &str { &self.name() } - #[throws(fmt::Result)] - fn format(&self, ts: Timestamp, f: &mut Formatter) { + #[throws(fmt::Error)] + fn format(&self, ts: Timestamp, f: &mut W) { write!(f, "TS{}(@{:?})", ts, &self); /* #[derive(Error,Debug)] @@ -61,13 +69,15 @@ impl TimeFormatter for ChronoTz { static memo: RwLock>> = const_rwlock(None); impl FromStr for Timezone { + type Err = !; + #[throws(!)] fn from_str(name: &str) -> Self { - let get = |memor,s| memor?.get(name).map(Clone::clone); - if let Some(got) = get(memo.read(), s) { return got } + let get = |memor| memor?.get(name).map(Clone::clone); + if let Some(got) = get(memo(), name) { return got } // slow path let memow = memo.write(); - if let Some(got) = get(memow, s) { return got } + if let Some(got) = get(memow) { return got } // really slow path let name = name.to_string(); @@ -78,11 +88,18 @@ impl FromStr for Timezone { Err(emsg) => { error!("Error loading timezone {:?}: {}, using UTC", name, emsg); let ctz = chrono::offset::Utc; - Arc::new(ChroniTz { ctz, name }) + Arc::new(ChronoTz { ctz, name }) }, }; - meow.get_or_insert_with(default) - .set(name.to_string(), r.clone()); + memow.get_or_insert_with(default) + .set(name.to_string(), out.clone()); out } } + +impl Default for Timezone { + fn default() -> Self { + let ctz = chrono::offset::Utc; + Arc::new(ChronoTz { ctz, name: default() }) + } +} -- 2.30.2