chiark / gitweb /
before enum for timezone
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 16 Oct 2020 00:20:39 +0000 (01:20 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 16 Oct 2020 00:20:39 +0000 (01:20 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/gamestate.rs
src/imports.rs
src/lib.rs
src/tz.rs

index 62f9df63fcb906db7689e2ee4a739abb83aa271b..792b76cd0d09dd5b4e5d51bc05a5d6bb0102c456 100644 (file)
@@ -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
   }
 }
index 6c293f41f62501de38c5e974563942bfae1d81d9..53b6f9939876e0c254b2506ede9b74d36eaeeab4 100644 (file)
@@ -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>() -> T { Default::default() }
+
 pub enum Impossible { }
 
 pub type E = anyhow::Error;
index 96163a0a76be090b9eb90abed37eb2e3626122c7..d095695af788ed78dc94ba33ee588f08810edce0 100644 (file)
@@ -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)]
 
index 9d75520ea3d13fa3aa294eb9f8efef15a6f049bb..81cbda5584ed16ab88e58e5b50925818fac71983 100644 (file)
--- 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<dyn TimeFormatter>);
 
-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<TZ: chrono::TimeZone> {
   ctz: TZ,
 }
   
-impl TimeFormatter<TZ: chrono::TimeZone> for ChronoTz<TZ> {
+impl<TZ: chrono::TimeZone + Debug + Sync + Send> TimeFormatter for ChronoTz<TZ> {
   fn name(&self) -> &str { &self.name() }
 
-  #[throws(fmt::Result)]
-  fn format(&self, ts: Timestamp, f: &mut Formatter) {
+  #[throws(fmt::Error)]
+  fn format<W: io::Write>(&self, ts: Timestamp, f: &mut W) {
     write!(f, "TS{}(@{:?})", ts, &self);
 
 /*    #[derive(Error,Debug)]
@@ -61,13 +69,15 @@ impl TimeFormatter<TZ: chrono::TimeZone> for ChronoTz<TZ> {
 static memo: RwLock<Option<HashMap<String, Timezone>>> = 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() })
+  }
+}