chiark / gitweb /
move geometry to base
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 24 Mar 2021 23:59:34 +0000 (23:59 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Mar 2021 00:35:57 +0000 (00:35 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
15 files changed:
Cargo.lock
Cargo.toml
base/Cargo.toml
base/geometry.rs [moved from src/geometry.rs with 69% similarity]
base/imports.rs
base/lib.rs
base/misc.rs
base/prelude.rs
src/error.rs
src/hidden.rs
src/imports.rs
src/lib.rs
src/prelude.rs
src/spec.rs
src/utils.rs

index 305dc2a6c6eac5d3c9b338372241af724d1f1942..cb97129232860b5a699bb7a2ac009ec376818e37 100644 (file)
@@ -2113,7 +2113,6 @@ dependencies = [
  "htmlescape",
  "index_vec",
  "inventory",
- "itertools",
  "lazy-init",
  "lazy_static",
  "libc",
@@ -2145,7 +2144,6 @@ dependencies = [
  "uds",
  "url 2.2.1",
  "vecdeque-stableix",
- "void",
 ]
 
 [[package]]
@@ -2172,9 +2170,13 @@ dependencies = [
  "derive_more",
  "fehler",
  "if_chain",
+ "itertools",
+ "num-derive",
+ "num-traits",
  "serde",
  "serde_with",
  "thiserror",
+ "void",
 ]
 
 [[package]]
index 819dba54cfb8e80980fc05f85c1e224f5ede6cea..5dec844ee20852956cd6d29357671945cab41d1a 100644 (file)
@@ -19,7 +19,6 @@ fs2="0.4"
 glob="0.3"
 htmlescape="0.3"
 inventory="0.1"
-itertools="0.10"
 lazy-init="0.5"
 lazy_static="1"
 libc="0.2"
@@ -47,7 +46,6 @@ typetag="0.1.6"
 uds="0.2"
 url="2"
 vecdeque-stableix="1"
-void="1"
 
 [dependencies.enum-map]
 version="0.6"
index 6107bb781b664dd187d1f97cf7b40070d07389c8..88dfa9b26512ab5f9b38778e9001d50777c435a1 100644 (file)
@@ -16,6 +16,10 @@ path = "lib.rs"
 arrayvec = "0.5"
 derive_more = "0.99"
 if_chain = "1"
+itertools="0.10"
+num-derive="0.3"
+num-traits="0.2"
+void="1"
 
 # Repeated in other Cargo.toml's because importing does not work properly
 fehler = "1"
similarity index 69%
rename from src/geometry.rs
rename to base/geometry.rs
index 4b58c7ceebf44fa805f4feb5fd43f4175b2fb880..a18a03dba640dd2ad787ff8f02ca7b904b52106f 100644 (file)
@@ -2,11 +2,12 @@
 // SPDX-License-Identifier: AGPL-3.0-or-later
 // There is NO WARRANTY.
 
-use crate::imports::*;
 use crate::prelude::*;
 
 use std::ops::{Add,Sub,Mul,Neg};
 
+use num_traits::NumCast;
+
 //---------- common types ----------
 
 pub type Coord = i32;
@@ -23,6 +24,61 @@ pub type Pos = PosC<Coord>;
 pub struct AreaC<T>(pub [PosC<T>; 2]);
 pub type Area = AreaC<Coord>;
 
+// ---------- CheckedArith ----------
+
+#[derive(Error,Clone,Copy,Debug,Serialize,Deserialize)]
+#[error("error parsing Z coordinate")]
+pub struct CoordinateOverflow;
+
+pub trait CheckedArith: Copy + Clone + Debug + 'static {
+  fn checked_add(self, rhs: Self) -> Result<Self, CoordinateOverflow>;
+  fn checked_sub(self, rhs: Self) -> Result<Self, CoordinateOverflow>;
+  fn checked_neg(self)            -> Result<Self, CoordinateOverflow>;
+}
+pub trait CheckedArithMul<RHS: Copy + Clone + Debug + 'static>:
+                               Copy + Clone + Debug + 'static {
+  fn checked_mul(self, rhs: RHS) -> Result<Self, CoordinateOverflow>;
+}
+
+macro_rules! checked_inherent { {$n:ident($($formal:tt)*) $($actual:tt)*} => {
+  fn $n(self $($formal)*) -> Result<Self, CoordinateOverflow> {
+    self.$n($($actual)*).ok_or(CoordinateOverflow)
+  }
+} }
+
+impl CheckedArith for i32 {
+  checked_inherent!{checked_add(, rhs: Self) rhs}
+  checked_inherent!{checked_sub(, rhs: Self) rhs}
+  checked_inherent!{checked_neg(           )    }
+}
+impl CheckedArithMul<i32> for i32 {
+  checked_inherent!{checked_mul(, rhs: Self) rhs}
+}
+impl CheckedArithMul<f64> for i32 {
+  fn checked_mul(self, rhs: f64) -> Result<Self, CoordinateOverflow> {
+    let lhs: f64 = self.into();
+    let out: f64 = lhs.checked_mul(rhs)?;
+    let out: Self = NumCast::from(out).ok_or(CoordinateOverflow)?;
+    Ok(out)
+  }
+}
+
+macro_rules! checked_float { {$n:ident($($formal:tt)*) $($modify:tt)*} => {
+  fn $n(self $($formal)*) -> Result<Self, CoordinateOverflow> {
+    let out = self $($modify)*;
+    if out.is_finite() { Ok(out) } else { Err(CoordinateOverflow) }
+  }
+} }
+
+impl CheckedArith for f64 {
+  checked_float!{checked_add(, rhs: Self)  + rhs }
+  checked_float!{checked_sub(, rhs: Self)  - rhs }
+  checked_float!{checked_neg()              .neg()}
+}
+impl CheckedArithMul<f64> for f64 {
+  checked_float!{checked_mul(, rhs: Self)  * rhs }
+}
+
 //---------- Pos ----------
 
 pub trait Mean { fn mean(&self, other: &Self) -> Self; }
index 3475488a4fc5e39f5c3dd9b98e65706f82781a8d..b2943caccd2a916693b48c2dfa6484f31e019977 100644 (file)
@@ -5,4 +5,7 @@
 pub use arrayvec;
 pub use derive_more;
 pub use if_chain;
+pub use itertools;
+pub use num_derive;
 pub use thiserror;
+pub use void;
index 753b5ce33063d69a559d8dd9d72b5ee83d27ea11..9dcb9b9dd02fb2a9aa8c048167c96f97f55c014f 100644 (file)
@@ -5,5 +5,6 @@
 pub mod imports;
 pub mod prelude;
 
+pub mod geometry;
 pub mod zcoord;
 pub mod misc;
index e2d170dd995d38ae1ce607e7292e6fd661a20057..c7abfc6387f90706f43553df8369c5f3807c5d9b 100644 (file)
@@ -5,8 +5,7 @@
 // This is in this crate for convenience, not because it's to do with
 // Z coordinates.
 
-use arrayvec::ArrayVec;
-use if_chain::if_chain;
+use crate::prelude::*;
 
 pub fn timestring_abbreviate<'x>(base: &str, this: &'x str)
                                  -> (&'x str, bool)
@@ -32,3 +31,16 @@ pub fn raw_angle_transform(compass: u8) -> String {
 }
 
 pub fn default<T:Default>() -> T { Default::default() }
+
+#[macro_export]
+macro_rules! display_as_debug {
+  {$x:ty $( , $($gen_tt:tt)* )?} => {
+    impl $( $($gen_tt)* )? std::fmt::Display for $x {
+      #[throws(std::fmt::Error)]
+      fn fmt(&self, f: &mut std::fmt::Formatter) {
+        <Self as Debug>::fmt(self, f)?
+      }
+    }
+  }
+}
+pub use crate::display_as_debug;
index 4e1b8e5b5a71256483782e8ef246970fb0e852ae..398da67373df01c44e362ab7c790a94a5d9758a6 100644 (file)
@@ -11,11 +11,17 @@ pub use std::num::{TryFromIntError, Wrapping};
 pub use std::str;
 pub use std::str::FromStr;
 
+pub use arrayvec::ArrayVec;
 pub use derive_more::*;
 pub use fehler::{throw, throws};
+pub use if_chain::if_chain;
+pub use itertools::izip;
 pub use serde::{Deserialize, Serialize};
 pub use serde_with::DeserializeFromStr;
 pub use serde_with::SerializeDisplay;
 pub use thiserror::Error;
+pub use void::Void;
 
+pub use crate::geometry::CoordinateOverflow;
 pub use crate::misc::default;
+pub use crate::misc::display_as_debug;
index 789921ad87df8f5de8f88447ba65c2ae7b11c230..365c7ed00ed5a511a8813cb0e8e2160c22d48845 100644 (file)
@@ -263,19 +263,6 @@ impl IdForById for PieceId {
   const ERROR: POE = POE::PieceGone;
 }
 
-#[macro_export]
-macro_rules! display_as_debug {
-  {$x:ty $( , $($gen_tt:tt)* )?} => {
-    impl $( $($gen_tt)* )? std::fmt::Display for $x {
-      #[throws(std::fmt::Error)]
-      fn fmt(&self, f: &mut std::fmt::Formatter) {
-        <Self as Debug>::fmt(self, f)?
-      }
-    }
-  }
-}
-pub use crate::display_as_debug;
-
 #[macro_export]
 macro_rules! error_from_losedetails {
   {$to:ty, $variant:ident, $from:ty} => {
index 77ba1f8de63b7f0928b6504e84cc1926d85369b5..289e3c009b15ee409f95c201aec3b3937ac0701b 100644 (file)
@@ -2,6 +2,7 @@
 // SPDX-License-Identifier: AGPL-3.0-or-later
 // There is NO WARRANTY.
 
+use crate::imports::*;
 use crate::prelude::*;
 
 #[path="vpid.rs"] mod vpid;
index c96f35262e87414d8e018bbd460b744f185d157c..dfd6d4edb7d566d302f080df30752d34486e6659 100644 (file)
@@ -23,11 +23,9 @@ pub use index_vec;
 pub use lazy_init;
 pub use lazy_static;
 pub use inventory;
-pub use itertools;
 pub use libc;
 pub use log;
 pub use nix;
-pub use num_derive;
 pub use once_cell;
 pub use ordered_float;
 pub use parking_lot;
@@ -39,4 +37,3 @@ pub use slotmap;
 pub use toml;
 pub use uds;
 pub use vecdeque_stableix;
-pub use void;
index 66ad53da8134738dc5ca0a2eb2a068d803393378..5ddbdc100633de61338e6f65ee8cb4a88803cc99 100644 (file)
@@ -16,7 +16,6 @@ pub mod config;
 pub mod deck;
 pub mod debugreader;
 pub mod error;
-pub mod geometry;
 pub mod gamestate;
 pub mod global;
 pub mod hand;
index 657ad455b4110f158b2f8c42bc91d633f2f34392..877d1ba64113f9af36943dfff10ece64bd0a17a3 100644 (file)
@@ -101,9 +101,12 @@ use nix::time::ClockId;
 pub const CLOCK_REALTIME : ClockId = ClockId::CLOCK_REALTIME ;
 pub const CLOCK_MONOTONIC: ClockId = ClockId::CLOCK_MONOTONIC;
 
+pub use otter_base::geometry::{Coord,Pos,PosC,Area,AreaC};
+pub use otter_base::geometry::CoordinateOverflow;
 pub use otter_base::zcoord::{self, ZCoord};
 pub use otter_base::misc as base_misc;
 pub use base_misc::default;
+pub use base_misc::display_as_debug;
 
 pub use crate::dbgc;
 pub use crate::{deref_to_field, deref_to_field_mut};
@@ -125,7 +128,6 @@ pub use crate::debugreader::DebugReader;
 pub use crate::error::*;
 pub use crate::fake_rng::*;
 pub use crate::gamestate::*;
-pub use crate::geometry::{Coord,Pos,PosC,Area,AreaC};
 pub use crate::global::*;
 pub use crate::hidden::*;
 pub use crate::keydata::*;
index 10c819d0a82ad13e79fd011e0f9053b32dfa10b6..f1fa845ca249a5109f403823507a45cfec9ca3aa 100644 (file)
@@ -20,10 +20,11 @@ use serde::{Deserialize, Serialize};
 use strum::{EnumString, Display};
 use thiserror::Error;
 
+use otter_base::geometry::{Coord,Pos};
+use otter_base::misc::display_as_debug;
+
 use crate::accounts::AccountName;
-use crate::error::display_as_debug;
 use crate::gamestate::PieceSpec;
-use crate::geometry::{Coord,Pos};
 use crate::prelude::default;
 
 pub use implementation::PlayerAccessSpec;
index 6702b19473b4643e22eb61e957ac3e0b17e52167..afa0120929904525255ee5dbc0be5917bb4d3f3d 100644 (file)
@@ -5,8 +5,6 @@
 use crate::imports::*;
 use crate::prelude::*;
 
-use std::ops::Neg;
-
 #[macro_export]
 macro_rules! ensure_eq {
   ($v1:expr, $v2:expr) => {
@@ -180,59 +178,6 @@ impl<Y: Sync, E: Sync, F: Sync + FnOnce() -> Result<Y,E>>
 #[error("error parsing Z coordinate")]
 pub struct FooParseError;
 
-#[derive(Error,Clone,Copy,Debug,Serialize,Deserialize)]
-#[error("error parsing Z coordinate")]
-pub struct CoordinateOverflow;
-
-pub trait CheckedArith: Copy + Clone + Debug + 'static {
-  fn checked_add(self, rhs: Self) -> Result<Self, CoordinateOverflow>;
-  fn checked_sub(self, rhs: Self) -> Result<Self, CoordinateOverflow>;
-  fn checked_neg(self)            -> Result<Self, CoordinateOverflow>;
-}
-pub trait CheckedArithMul<RHS: Copy + Clone + Debug + 'static>:
-                               Copy + Clone + Debug + 'static {
-  fn checked_mul(self, rhs: RHS) -> Result<Self, CoordinateOverflow>;
-}
-
-macro_rules! checked_inherent { {$n:ident($($formal:tt)*) $($actual:tt)*} => {
-  fn $n(self $($formal)*) -> Result<Self, CoordinateOverflow> {
-    self.$n($($actual)*).ok_or(CoordinateOverflow)
-  }
-} }
-
-impl CheckedArith for i32 {
-  checked_inherent!{checked_add(, rhs: Self) rhs}
-  checked_inherent!{checked_sub(, rhs: Self) rhs}
-  checked_inherent!{checked_neg(           )    }
-}
-impl CheckedArithMul<i32> for i32 {
-  checked_inherent!{checked_mul(, rhs: Self) rhs}
-}
-impl CheckedArithMul<f64> for i32 {
-  fn checked_mul(self, rhs: f64) -> Result<Self, CoordinateOverflow> {
-    let lhs: f64 = self.into();
-    let out: f64 = lhs.checked_mul(rhs)?;
-    let out: Self = num::NumCast::from(out).ok_or(CoordinateOverflow)?;
-    Ok(out)
-  }
-}
-
-macro_rules! checked_float { {$n:ident($($formal:tt)*) $($modify:tt)*} => {
-  fn $n(self $($formal)*) -> Result<Self, CoordinateOverflow> {
-    let out = self $($modify)*;
-    if out.is_finite() { Ok(out) } else { Err(CoordinateOverflow) }
-  }
-} }
-
-impl CheckedArith for f64 {
-  checked_float!{checked_add(, rhs: Self)  + rhs }
-  checked_float!{checked_sub(, rhs: Self)  - rhs }
-  checked_float!{checked_neg()              .neg()}
-}
-impl CheckedArithMul<f64> for f64 {
-  checked_float!{checked_mul(, rhs: Self)  * rhs }
-}
-
 pub mod timespec_serde {
   use super::*;