From dc8c122bf6c5eeeb97238e92a1dee7a953d5c565 Mon Sep 17 00:00:00 2001 From: Gero Gerke <11deutron11@gmail.com> Date: Fri, 11 Oct 2019 15:55:16 +0200 Subject: [PATCH 1/4] Replace ToString with Into --- src/client/mod.rs | 16 ++++++++-------- src/query/mod.rs | 8 ++++---- src/query/read_query.rs | 8 ++++---- src/query/write_query.rs | 16 ++++++++-------- tests/integration_tests.rs | 16 +++++++++------- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index e24f394..66e93b6 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -81,12 +81,12 @@ impl Client { /// ``` pub fn new(url: S1, database: S2) -> Self where - S1: ToString, - S2: ToString, + S1: Into, + S2: Into, { Client { - url: url.to_string(), - database: database.to_string(), + url: url.into(), + database: database.into(), auth: None, } } @@ -107,12 +107,12 @@ impl Client { /// ``` pub fn with_auth(mut self, username: S1, password: S2) -> Self where - S1: ToString, - S2: ToString, + S1: Into, + S2: Into, { self.auth = Some(Authentication { - username: username.to_string(), - password: password.to_string(), + username: username.into(), + password: password.into(), }); self } diff --git a/src/query/mod.rs b/src/query/mod.rs index a052acb..b94f4aa 100644 --- a/src/query/mod.rs +++ b/src/query/mod.rs @@ -99,7 +99,7 @@ impl dyn Query { /// ``` pub fn write_query(timestamp: Timestamp, measurement: S) -> WriteQuery where - S: ToString, + S: Into, { WriteQuery::new(timestamp, measurement) } @@ -115,7 +115,7 @@ impl dyn Query { /// ``` pub fn raw_read_query(read_query: S) -> ReadQuery where - S: ToString, + S: Into, { ReadQuery::new(read_query) } @@ -131,10 +131,10 @@ impl ValidQuery { } impl From for ValidQuery where - T: ToString, + T: Into, { fn from(string: T) -> Self { - Self(string.to_string()) + Self(string.into()) } } impl PartialEq for ValidQuery { diff --git a/src/query/read_query.rs b/src/query/read_query.rs index 1068fa7..7ee0b43 100644 --- a/src/query/read_query.rs +++ b/src/query/read_query.rs @@ -13,19 +13,19 @@ impl ReadQuery { /// Creates a new [`ReadQuery`] pub fn new(query: S) -> Self where - S: ToString, + S: Into, { ReadQuery { - queries: vec![query.to_string()], + queries: vec![query.into()], } } /// Adds a query to the [`ReadQuery`] pub fn add_query(mut self, query: S) -> Self where - S: ToString, + S: Into, { - self.queries.push(query.to_string()); + self.queries.push(query.into()); self } } diff --git a/src/query/write_query.rs b/src/query/write_query.rs index 94561dd..ab27b7f 100644 --- a/src/query/write_query.rs +++ b/src/query/write_query.rs @@ -19,12 +19,12 @@ impl WriteQuery { /// Creates a new [`WriteQuery`](crate::query::write_query::WriteQuery) pub fn new(timestamp: Timestamp, measurement: S) -> Self where - S: ToString, + S: Into, { WriteQuery { fields: vec![], tags: vec![], - measurement: measurement.to_string(), + measurement: measurement.into(), timestamp, } } @@ -40,11 +40,11 @@ impl WriteQuery { /// ``` pub fn add_field(mut self, tag: S, value: I) -> Self where - S: ToString, + S: Into, I: Into, { let val: Type = value.into(); - self.fields.push((tag.to_string(), val.to_string())); + self.fields.push((tag.into(), val.to_string())); self } @@ -63,11 +63,11 @@ impl WriteQuery { /// ``` pub fn add_tag(mut self, tag: S, value: I) -> Self where - S: ToString, + S: Into, I: Into, { let val: Type = value.into(); - self.tags.push((tag.to_string(), val.to_string())); + self.tags.push((tag.into(), val.to_string())); self } @@ -93,8 +93,8 @@ pub enum Type { Text(String), } -impl ToString for Type { - fn to_string(&self) -> String { +impl Into for Type { + fn into(self) -> String { use Type::*; match self { diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index a1d5dc0..ba1d99f 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -20,7 +20,7 @@ fn get_runtime() -> Runtime { fn create_client(db_name: T) -> Client where - T: ToString, + T: Into, { Client::new("http://localhost:8086", db_name) } @@ -35,19 +35,21 @@ impl Drop for RunOnDrop { } } -fn create_db(test_name: T) -> Result +fn create_db(name: T) -> Result where - T: ToString, + T: Into, { - let query = format!("CREATE DATABASE {}", test_name.to_string()); + let test_name = name.into(); + let query = format!("CREATE DATABASE {}", test_name); get_runtime().block_on(create_client(test_name).query(&Query::raw_read_query(query))) } -fn delete_db(test_name: T) -> Result +fn delete_db(name: T) -> Result where - T: ToString, + T: Into, { - let query = format!("DROP DATABASE {}", test_name.to_string()); + let test_name = name.into(); + let query = format!("DROP DATABASE {}", test_name); get_runtime().block_on(create_client(test_name).query(&Query::raw_read_query(query))) } From 42aa3a34ae6a8c50d1cb3a3235a68b8be0af1034 Mon Sep 17 00:00:00 2001 From: Gero Gerke <11deutron11@gmail.com> Date: Fri, 11 Oct 2019 16:04:01 +0200 Subject: [PATCH 2/4] Fix failing build --- src/query/write_query.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/query/write_query.rs b/src/query/write_query.rs index ab27b7f..d54bced 100644 --- a/src/query/write_query.rs +++ b/src/query/write_query.rs @@ -44,7 +44,7 @@ impl WriteQuery { I: Into, { let val: Type = value.into(); - self.fields.push((tag.into(), val.to_string())); + self.fields.push((tag.into(), val.into())); self } @@ -67,7 +67,7 @@ impl WriteQuery { I: Into, { let val: Type = value.into(); - self.tags.push((tag.into(), val.to_string())); + self.tags.push((tag.into(), val.into())); self } From d305ac1d4c5a1830c5a21c1ca4f3755795f80a8c Mon Sep 17 00:00:00 2001 From: Gero Gerke <11deutron11@gmail.com> Date: Fri, 11 Oct 2019 16:13:03 +0200 Subject: [PATCH 3/4] Use Display trait for Type instead of Into --- src/query/write_query.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/query/write_query.rs b/src/query/write_query.rs index d54bced..2b151bb 100644 --- a/src/query/write_query.rs +++ b/src/query/write_query.rs @@ -4,6 +4,7 @@ use crate::query::{QueryType, ValidQuery}; use crate::{Error, Query, Timestamp}; +use std::fmt::{Display, Formatter}; // todo: batch write queries @@ -44,7 +45,7 @@ impl WriteQuery { I: Into, { let val: Type = value.into(); - self.fields.push((tag.into(), val.into())); + self.fields.push((tag.into(), val.to_string())); self } @@ -67,7 +68,7 @@ impl WriteQuery { I: Into, { let val: Type = value.into(); - self.tags.push((tag.into(), val.into())); + self.tags.push((tag.into(), val.to_string())); self } @@ -93,17 +94,18 @@ pub enum Type { Text(String), } -impl Into for Type { - fn into(self) -> String { +impl Display for Type { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { use Type::*; - match self { + let displayed = match self { Boolean(x) => x.to_string(), Float(x) => x.to_string(), SignedInteger(x) => x.to_string(), UnsignedInteger(x) => x.to_string(), Text(text) => format!("\"{text}\"", text = text), - } + }; + write!(f, "{}", displayed) } } From c08906282e5e6f5458b32a5cc098029f204324e3 Mon Sep 17 00:00:00 2001 From: Gero Gerke <11deutron11@gmail.com> Date: Fri, 11 Oct 2019 16:18:32 +0200 Subject: [PATCH 4/4] Code Review Feedback --- src/query/write_query.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/query/write_query.rs b/src/query/write_query.rs index 2b151bb..3f74e1a 100644 --- a/src/query/write_query.rs +++ b/src/query/write_query.rs @@ -98,14 +98,13 @@ impl Display for Type { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { use Type::*; - let displayed = match self { - Boolean(x) => x.to_string(), - Float(x) => x.to_string(), - SignedInteger(x) => x.to_string(), - UnsignedInteger(x) => x.to_string(), - Text(text) => format!("\"{text}\"", text = text), - }; - write!(f, "{}", displayed) + match self { + Boolean(x) => write!(f, "{}", x), + Float(x) => write!(f, "{}", x), + SignedInteger(x) => write!(f, "{}", x), + UnsignedInteger(x) => write!(f, "{}", x), + Text(text) => write!(f, "\"{text}\"", text = text), + } } }