From: Ian Jackson Date: Wed, 24 Mar 2021 23:59:34 +0000 (+0000) Subject: move geometry to base X-Git-Tag: otter-0.5.0~435 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=7a1e1f832da42e29560c183546bb117674d3699b;p=otter.git move geometry to base Signed-off-by: Ian Jackson --- diff --git a/Cargo.lock b/Cargo.lock index 305dc2a6..cb971292 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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]] diff --git a/Cargo.toml b/Cargo.toml index 819dba54..5dec844e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/base/Cargo.toml b/base/Cargo.toml index 6107bb78..88dfa9b2 100644 --- a/base/Cargo.toml +++ b/base/Cargo.toml @@ -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" diff --git a/src/geometry.rs b/base/geometry.rs similarity index 69% rename from src/geometry.rs rename to base/geometry.rs index 4b58c7ce..a18a03db 100644 --- a/src/geometry.rs +++ b/base/geometry.rs @@ -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; pub struct AreaC(pub [PosC; 2]); pub type Area = AreaC; +// ---------- 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; + fn checked_sub(self, rhs: Self) -> Result; + fn checked_neg(self) -> Result; +} +pub trait CheckedArithMul: + Copy + Clone + Debug + 'static { + fn checked_mul(self, rhs: RHS) -> Result; +} + +macro_rules! checked_inherent { {$n:ident($($formal:tt)*) $($actual:tt)*} => { + fn $n(self $($formal)*) -> Result { + 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 for i32 { + checked_inherent!{checked_mul(, rhs: Self) rhs} +} +impl CheckedArithMul for i32 { + fn checked_mul(self, rhs: f64) -> Result { + 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 { + 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 for f64 { + checked_float!{checked_mul(, rhs: Self) * rhs } +} + //---------- Pos ---------- pub trait Mean { fn mean(&self, other: &Self) -> Self; } diff --git a/base/imports.rs b/base/imports.rs index 3475488a..b2943cac 100644 --- a/base/imports.rs +++ b/base/imports.rs @@ -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; diff --git a/base/lib.rs b/base/lib.rs index 753b5ce3..9dcb9b9d 100644 --- a/base/lib.rs +++ b/base/lib.rs @@ -5,5 +5,6 @@ pub mod imports; pub mod prelude; +pub mod geometry; pub mod zcoord; pub mod misc; diff --git a/base/misc.rs b/base/misc.rs index e2d170dd..c7abfc63 100644 --- a/base/misc.rs +++ b/base/misc.rs @@ -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::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) { + ::fmt(self, f)? + } + } + } +} +pub use crate::display_as_debug; diff --git a/base/prelude.rs b/base/prelude.rs index 4e1b8e5b..398da673 100644 --- a/base/prelude.rs +++ b/base/prelude.rs @@ -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; diff --git a/src/error.rs b/src/error.rs index 789921ad..365c7ed0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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) { - ::fmt(self, f)? - } - } - } -} -pub use crate::display_as_debug; - #[macro_export] macro_rules! error_from_losedetails { {$to:ty, $variant:ident, $from:ty} => { diff --git a/src/hidden.rs b/src/hidden.rs index 77ba1f8d..289e3c00 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -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; diff --git a/src/imports.rs b/src/imports.rs index c96f3526..dfd6d4ed 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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; diff --git a/src/lib.rs b/src/lib.rs index 66ad53da..5ddbdc10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/prelude.rs b/src/prelude.rs index 657ad455..877d1ba6 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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::*; diff --git a/src/spec.rs b/src/spec.rs index 10c819d0..f1fa845c 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -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; diff --git a/src/utils.rs b/src/utils.rs index 6702b194..afa01209 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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 Result> #[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; - fn checked_sub(self, rhs: Self) -> Result; - fn checked_neg(self) -> Result; -} -pub trait CheckedArithMul: - Copy + Clone + Debug + 'static { - fn checked_mul(self, rhs: RHS) -> Result; -} - -macro_rules! checked_inherent { {$n:ident($($formal:tt)*) $($actual:tt)*} => { - fn $n(self $($formal)*) -> Result { - 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 for i32 { - checked_inherent!{checked_mul(, rhs: Self) rhs} -} -impl CheckedArithMul for i32 { - fn checked_mul(self, rhs: f64) -> Result { - 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 { - 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 for f64 { - checked_float!{checked_mul(, rhs: Self) * rhs } -} - pub mod timespec_serde { use super::*;