From 5eed3d7f719482e8eb8ad30012b1208e6fee9870 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 26 Mar 2020 13:50:20 +1000 Subject: [PATCH] use a macro for newtype defs --- rslib/src/storage/mod.rs | 2 -- rslib/src/storage/timestamp.rs | 40 ---------------------------------- rslib/src/storage/usn.rs | 24 -------------------- rslib/src/timestamp.rs | 10 +++------ rslib/src/types.rs | 36 +++++++++++++++++++++++++----- 5 files changed, 33 insertions(+), 79 deletions(-) delete mode 100644 rslib/src/storage/timestamp.rs delete mode 100644 rslib/src/storage/usn.rs diff --git a/rslib/src/storage/mod.rs b/rslib/src/storage/mod.rs index 1fe26fcb7..2ed04892c 100644 --- a/rslib/src/storage/mod.rs +++ b/rslib/src/storage/mod.rs @@ -1,5 +1,3 @@ mod sqlite; -mod timestamp; -mod usn; pub(crate) use sqlite::{SqliteStorage, StorageContext}; diff --git a/rslib/src/storage/timestamp.rs b/rslib/src/storage/timestamp.rs deleted file mode 100644 index 74fc40965..000000000 --- a/rslib/src/storage/timestamp.rs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright: Ankitects Pty Ltd and contributors -// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - -use crate::timestamp::{TimestampMillis, TimestampSecs}; -use rusqlite::{ - types::{FromSql, FromSqlError, ToSqlOutput, Value, ValueRef}, - ToSql, -}; - -impl FromSql for TimestampSecs { - fn column_result(value: ValueRef<'_>) -> std::result::Result { - if let ValueRef::Integer(i) = value { - Ok(TimestampSecs(i)) - } else { - Err(FromSqlError::InvalidType) - } - } -} - -impl ToSql for TimestampSecs { - fn to_sql(&self) -> rusqlite::Result> { - Ok(ToSqlOutput::Owned(Value::Integer(self.0))) - } -} - -impl FromSql for TimestampMillis { - fn column_result(value: ValueRef<'_>) -> std::result::Result { - if let ValueRef::Integer(i) = value { - Ok(TimestampMillis(i)) - } else { - Err(FromSqlError::InvalidType) - } - } -} - -impl ToSql for TimestampMillis { - fn to_sql(&self) -> rusqlite::Result> { - Ok(ToSqlOutput::Owned(Value::Integer(self.0))) - } -} diff --git a/rslib/src/storage/usn.rs b/rslib/src/storage/usn.rs deleted file mode 100644 index d911604af..000000000 --- a/rslib/src/storage/usn.rs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright: Ankitects Pty Ltd and contributors -// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - -use crate::types::Usn; -use rusqlite::{ - types::{FromSql, FromSqlError, ToSqlOutput, Value, ValueRef}, - ToSql, -}; - -impl FromSql for Usn { - fn column_result(value: ValueRef<'_>) -> std::result::Result { - if let ValueRef::Integer(i) = value { - Ok(Self(i as i32)) - } else { - Err(FromSqlError::InvalidType) - } - } -} - -impl ToSql for Usn { - fn to_sql(&self) -> rusqlite::Result> { - Ok(ToSqlOutput::Owned(Value::Integer(self.0 as i64))) - } -} diff --git a/rslib/src/timestamp.rs b/rslib/src/timestamp.rs index 79d86685a..b1a419579 100644 --- a/rslib/src/timestamp.rs +++ b/rslib/src/timestamp.rs @@ -1,11 +1,11 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +use crate::define_newtype; use std::time; -#[repr(transparent)] -#[derive(Debug, Clone, Copy)] -pub struct TimestampSecs(pub i64); +define_newtype!(TimestampSecs, i64); +define_newtype!(TimestampMillis, i64); impl TimestampSecs { pub fn now() -> Self { @@ -13,10 +13,6 @@ impl TimestampSecs { } } -#[repr(transparent)] -#[derive(Debug, Clone, Copy)] -pub struct TimestampMillis(pub i64); - impl TimestampMillis { pub fn now() -> Self { Self(elapsed().as_millis() as i64) diff --git a/rslib/src/types.rs b/rslib/src/types.rs index 8912fd3cf..1d3fef25a 100644 --- a/rslib/src/types.rs +++ b/rslib/src/types.rs @@ -1,11 +1,35 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -// while Anki tends to only use positive numbers, sqlite only supports -// signed integers, so these numbers are signed as well. - pub type ObjID = i64; -#[repr(transparent)] -#[derive(Debug, Clone, Copy)] -pub struct Usn(pub i32); +#[macro_export] +macro_rules! define_newtype { + ( $name:ident, $type:ident ) => { + #[repr(transparent)] + #[derive(Debug, Clone, Copy)] + pub struct $name(pub $type); + + impl rusqlite::types::FromSql for $name { + fn column_result( + value: rusqlite::types::ValueRef<'_>, + ) -> std::result::Result { + if let rusqlite::types::ValueRef::Integer(i) = value { + Ok(Self(i as $type)) + } else { + Err(rusqlite::types::FromSqlError::InvalidType) + } + } + } + + impl rusqlite::ToSql for $name { + fn to_sql(&self) -> ::rusqlite::Result> { + Ok(rusqlite::types::ToSqlOutput::Owned( + rusqlite::types::Value::Integer(self.0 as i64), + )) + } + } + }; +} + +define_newtype!(Usn, i32);