Skip to content

Commit

Permalink
use derive_more::From (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril authored Sep 13, 2023
1 parent 39e7298 commit 12a7e7f
Show file tree
Hide file tree
Showing 25 changed files with 124 additions and 213 deletions.
37 changes: 36 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ criterion = { version = "0.4.0", features = ["async", "async_tokio", "html_repor
crossbeam-channel = "0.5"
cursive = "0.20"
decorum = { version = "0.3.1", default-features = false, features = ["std"] }
derive_more = "0.99.17"
dirs = "5.0.1"
duct = "0.13.5"
email_address = "0.2.4"
Expand Down
1 change: 1 addition & 0 deletions crates/bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spacetimedb-bindings-sys = { path = "../bindings-sys", version = "0.6.1" }
spacetimedb-lib = { path = "../lib", default-features = false, version = "0.6.1"}
spacetimedb-bindings-macro = { path = "../bindings-macro", version = "0.6.1"}

derive_more.workspace = true
log.workspace = true
once_cell.workspace = true
scoped-tls.workspace = true
Expand Down
7 changes: 1 addition & 6 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,11 @@ pub struct IndexDef<'a> {
}

/// A table iterator which yields values of the `TableType` corresponding to the table.
#[derive(derive_more::From)]
pub struct TableIter<T: TableType> {
iter: TableTypeTableIter<T>,
}

impl<T: TableType> From<TableTypeTableIter<T>> for TableIter<T> {
fn from(iter: TableTypeTableIter<T>) -> Self {
Self { iter }
}
}

impl<T: TableType> Iterator for TableIter<T> {
type Item = T;

Expand Down
1 change: 1 addition & 0 deletions crates/client-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ bytes = "1"
bytestring = "1"
tokio-tungstenite = "0.18.0"
itoa = "1.0.9"
derive_more = "0.99.17"
58 changes: 22 additions & 36 deletions crates/client-api/src/routes/database.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
use std::collections::HashMap;
use std::sync::Arc;

use axum::body::Bytes;
use axum::extract::{DefaultBodyLimit, FromRef, Path, Query, State};
use axum::response::{ErrorResponse, IntoResponse};
use axum::{headers, TypedHeader};
use chrono::Utc;
use futures::StreamExt;
use http::StatusCode;
use rand::Rng;
use serde::Deserialize;
use serde_json::{json, Value};
use spacetimedb::address::Address;
use spacetimedb::auth::identity::encode_token;
use spacetimedb::database_logger::DatabaseLogger;
use spacetimedb::host::DescribedEntityType;
use spacetimedb::host::EntityDef;
use spacetimedb::host::ReducerArgs;
use spacetimedb::host::ReducerCallError;
use spacetimedb::host::ReducerOutcome;
use spacetimedb::host::UpdateDatabaseSuccess;
use spacetimedb_lib::name;
use spacetimedb_lib::name::DomainName;
use spacetimedb_lib::name::DomainParsingError;
use spacetimedb_lib::name::PublishOp;
use spacetimedb::identity::Identity;
use spacetimedb::json::client_api::StmtResultJson;
use spacetimedb::messages::control_db::{Database, DatabaseInstance, HostType};
use spacetimedb::sql::execute::execute;
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::name::{
self, DnsLookupResponse, DomainName, DomainParsingError, InsertDomainResult, PublishOp, PublishResult,
};
use spacetimedb_lib::recovery::{RecoveryCode, RecoveryCodeResponse};
use spacetimedb_lib::sats::WithTypespace;
use std::collections::HashMap;
use std::convert::From;
use std::sync::Arc;

use super::identity::IdentityForUrl;
use crate::auth::{
SpacetimeAuth, SpacetimeAuthHeader, SpacetimeEnergyUsed, SpacetimeExecutionDurationMicros, SpacetimeIdentity,
SpacetimeIdentityToken,
};
use spacetimedb::address::Address;
use spacetimedb::database_logger::DatabaseLogger;
use spacetimedb::host::DescribedEntityType;
use spacetimedb::identity::Identity;
use spacetimedb::json::client_api::StmtResultJson;
use spacetimedb::messages::control_db::{Database, DatabaseInstance, HostType};

use super::identity::IdentityForUrl;
use crate::util::{ByteStringBody, NameOrAddress};
use crate::{log_and_500, ControlCtx, ControlNodeDelegate, WorkerCtx};

#[derive(derive_more::From)]
pub(crate) struct DomainParsingRejection(pub(crate) DomainParsingError);
impl From<DomainParsingError> for DomainParsingRejection {
fn from(e: DomainParsingError) -> Self {
DomainParsingRejection(e)
}
}

impl IntoResponse for DomainParsingRejection {
fn into_response(self) -> axum::response::Response {
(StatusCode::BAD_REQUEST, "Unable to parse domain name").into_response()
Expand Down Expand Up @@ -147,28 +148,13 @@ fn reducer_outcome_response(identity: &Identity, reducer: &str, outcome: Reducer
}
}

#[derive(Debug)]
#[derive(Debug, derive_more::From)]
pub enum DBCallErr {
HandlerError(ErrorResponse),
NoSuchDatabase,
InstanceNotScheduled,
}

use chrono::Utc;
use rand::Rng;
use spacetimedb::auth::identity::encode_token;
use spacetimedb::sql::execute::execute;
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::name::{DnsLookupResponse, InsertDomainResult, PublishResult};
use spacetimedb_lib::recovery::{RecoveryCode, RecoveryCodeResponse};
use std::convert::From;

impl From<ErrorResponse> for DBCallErr {
fn from(error: ErrorResponse) -> Self {
DBCallErr::HandlerError(error)
}
}

pub struct DatabaseInformation {
database_instance: DatabaseInstance,
auth: SpacetimeAuth,
Expand Down
8 changes: 1 addition & 7 deletions crates/client-api/src/routes/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,9 @@ pub async fn get_identity(
/// This newtype around `Identity` implements `Deserialize`
/// directly from the inner identity bytes,
/// without the enclosing `ProductValue` wrapper.
#[derive(derive_more::Into)]
pub struct IdentityForUrl(Identity);

impl From<IdentityForUrl> for Identity {
/// Consumes `self` returning the backing `Identity`.
fn from(IdentityForUrl(id): IdentityForUrl) -> Identity {
id
}
}

impl<'de> serde::Deserialize<'de> for IdentityForUrl {
fn deserialize<D: serde::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
<_>::deserialize(de).map(|DeserializeWrapper(b)| IdentityForUrl(Identity::from_byte_array(b)))
Expand Down
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ bytes.workspace = true
bytestring.workspace = true
clap.workspace = true
crossbeam-channel.workspace = true
derive_more.workspace = true
dirs.workspace = true
email_address.workspace = true
flate2.workspace = true
Expand Down
15 changes: 2 additions & 13 deletions crates/core/src/client/client_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ops::Deref;
use crate::host::{ModuleHost, NoSuchModule, ReducerArgs, ReducerCallError, ReducerCallResult};
use crate::protobuf::client_api::Subscribe;
use crate::worker_metrics::{CONNECTED_CLIENTS, WEBSOCKET_SENT, WEBSOCKET_SENT_MSG_SIZE};
use derive_more::From;
use futures::prelude::*;
use tokio::sync::mpsc;

Expand Down Expand Up @@ -68,7 +69,7 @@ impl Deref for ClientConnection {
}
}

#[derive(Debug)]
#[derive(Debug, From)]
pub enum DataMessage {
Text(String),
Binary(Vec<u8>),
Expand All @@ -88,18 +89,6 @@ impl DataMessage {
}
}

impl From<String> for DataMessage {
fn from(text: String) -> Self {
DataMessage::Text(text)
}
}

impl From<Vec<u8>> for DataMessage {
fn from(bin: Vec<u8>) -> Self {
DataMessage::Binary(bin)
}
}

impl ClientConnection {
/// Returns an error if ModuleHost closed
pub async fn spawn<F, Fut>(
Expand Down
23 changes: 10 additions & 13 deletions crates/core/src/db/datastore/locking_tx_datastore/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
mod btree_index;
mod sequence;
mod table;

use self::{
btree_index::{BTreeIndex, BTreeIndexRangeIter},
sequence::Sequence,
table::Table,
};
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
ops::RangeBounds,
sync::Arc,
vec,
};

use super::{
system_tables::{
StColumnRow, StIndexRow, StSequenceRow, StTableRow, INDEX_ID_SEQUENCE_ID, SEQUENCE_ID_SEQUENCE_ID,
Expand Down Expand Up @@ -41,6 +35,8 @@ use crate::{
},
error::{DBError, IndexError, TableError},
};

use derive_more::Into;
use parking_lot::{lock_api::ArcMutexGuard, Mutex, RawMutex};
use spacetimedb_lib::{
auth::{StAccess, StTableType},
Expand All @@ -51,6 +47,12 @@ use spacetimedb_lib::{
use spacetimedb_sats::{
AlgebraicType, AlgebraicValue, BuiltinType, BuiltinValue, ProductType, ProductTypeElement, ProductValue,
};
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
ops::RangeBounds,
sync::Arc,
vec,
};
use thiserror::Error;

#[derive(Error, Debug, PartialEq, Eq)]
Expand All @@ -77,16 +79,11 @@ pub enum SequenceError {

const SEQUENCE_PREALLOCATION_AMOUNT: i128 = 4_096;

#[derive(Into)]
pub struct Data {
data: ProductValue,
}

impl From<Data> for ProductValue {
fn from(data: Data) -> Self {
data.data
}
}

impl traits::Data for Data {
fn view(&self) -> &ProductValue {
&self.data
Expand Down
6 changes: 6 additions & 0 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ impl DBError {
}
}

impl From<DBError> for ErrorVm {
fn from(err: DBError) -> Self {
ErrorVm::Other(err.into())
}
}

impl From<InvalidFieldError> for DBError {
fn from(value: InvalidFieldError) -> Self {
LibError::from(value).into()
Expand Down
Loading

1 comment on commit 12a7e7f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark for 12a7e7f

Click to view benchmark
Test Base PR %
empty reducer 25.6±1.42µs 24.2±1.58µs -5.47%
filter nonunique random/100 36.1±1.90µs 36.4±1.80µs +0.83%
filter nonunique random/1000 38.0±2.04µs 37.6±1.61µs -1.05%
filter nonunique random/10000 39.1±2.07µs 38.5±3.40µs -1.53%
filter nonunique sequential/100 37.7±2.11µs 37.0±2.23µs -1.86%
filter nonunique sequential/1000 37.8±1.73µs 38.2±1.75µs +1.06%
filter nonunique sequential/10000 38.3±2.15µs 37.7±1.85µs -1.57%
filter nonunique sequential/100000 41.0±2.61µs 38.1±1.69µs -7.07%
filter unique random/100 33.9±2.59µs 34.2±2.02µs +0.88%
filter unique random/1000 35.6±13.32µs 34.0±1.96µs -4.49%
filter unique random/10000 35.5±6.67µs 33.0±2.12µs -7.04%
filter unique sequential/100 34.6±3.07µs 34.1±2.18µs -1.45%
filter unique sequential/1000 34.0±1.21µs 33.7±2.02µs -0.88%
filter unique sequential/10000 33.9±1.72µs 33.3±1.67µs -1.77%
filter unique sequential/100000 32.8±1.82µs 34.6±2.58µs +5.49%
iterator/1_000_000 rows 568.4±2.42ms 540.9±3.19ms -4.84%
large input 1351.5±264.00µs 1305.8±181.18µs -3.38%
multi insert/10 67.7±9.57µs 66.6±5.84µs -1.62%
multi insert/100 338.1±14.22µs 314.8±8.62µs -6.89%
multi insert/1000 2.9±0.03ms 2.9±0.03ms 0.00%
multi insert/50 171.4±2.76µs 173.5±7.67µs +1.23%
multi insert/500 1473.3±10.87µs 1455.7±26.15µs -1.19%
multiple large arguments 12.7±0.17ms 12.6±0.14ms -0.79%
single insert 56.4±3.56µs 54.9±4.96µs -2.66%
with existing records/100000 54.6±3.93µs 47.4±7.74µs -13.19%
with existing records/1000000 60.7±3.70µs 63.2±3.04µs +4.12%
with existing records/200000 61.8±4.28µs 54.9±4.55µs -11.17%
with existing records/300000 57.0±3.41µs 58.8±4.05µs +3.16%
with existing records/400000 57.9±4.62µs 59.5±4.15µs +2.76%
with existing records/500000 60.8±5.72µs 60.1±4.10µs -1.15%
with existing records/600000 61.9±3.54µs 60.2±4.40µs -2.75%
with existing records/700000 60.6±6.56µs 62.8±6.34µs +3.63%
with existing records/800000 62.4±4.24µs 62.3±4.34µs -0.16%
with existing records/900000 61.7±3.98µs 61.7±4.47µs 0.00%

Please sign in to comment.