Skip to content

Commit 9f29439

Browse files
authored
Replace ToString with Into<String> (#30)
* Replace ToString with Into<String> * Use Display trait for Type instead of Into<String>
1 parent 4f1d4f6 commit 9f29439

File tree

5 files changed

+39
-36
lines changed

5 files changed

+39
-36
lines changed

src/client/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ impl Client {
8181
/// ```
8282
pub fn new<S1, S2>(url: S1, database: S2) -> Self
8383
where
84-
S1: ToString,
85-
S2: ToString,
84+
S1: Into<String>,
85+
S2: Into<String>,
8686
{
8787
Client {
88-
url: url.to_string(),
89-
database: database.to_string(),
88+
url: url.into(),
89+
database: database.into(),
9090
auth: None,
9191
}
9292
}
@@ -107,12 +107,12 @@ impl Client {
107107
/// ```
108108
pub fn with_auth<S1, S2>(mut self, username: S1, password: S2) -> Self
109109
where
110-
S1: ToString,
111-
S2: ToString,
110+
S1: Into<String>,
111+
S2: Into<String>,
112112
{
113113
self.auth = Some(Authentication {
114-
username: username.to_string(),
115-
password: password.to_string(),
114+
username: username.into(),
115+
password: password.into(),
116116
});
117117
self
118118
}

src/query/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl dyn Query {
9999
/// ```
100100
pub fn write_query<S>(timestamp: Timestamp, measurement: S) -> WriteQuery
101101
where
102-
S: ToString,
102+
S: Into<String>,
103103
{
104104
WriteQuery::new(timestamp, measurement)
105105
}
@@ -115,7 +115,7 @@ impl dyn Query {
115115
/// ```
116116
pub fn raw_read_query<S>(read_query: S) -> ReadQuery
117117
where
118-
S: ToString,
118+
S: Into<String>,
119119
{
120120
ReadQuery::new(read_query)
121121
}
@@ -131,10 +131,10 @@ impl ValidQuery {
131131
}
132132
impl<T> From<T> for ValidQuery
133133
where
134-
T: ToString,
134+
T: Into<String>,
135135
{
136136
fn from(string: T) -> Self {
137-
Self(string.to_string())
137+
Self(string.into())
138138
}
139139
}
140140
impl PartialEq<String> for ValidQuery {

src/query/read_query.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ impl ReadQuery {
1313
/// Creates a new [`ReadQuery`]
1414
pub fn new<S>(query: S) -> Self
1515
where
16-
S: ToString,
16+
S: Into<String>,
1717
{
1818
ReadQuery {
19-
queries: vec![query.to_string()],
19+
queries: vec![query.into()],
2020
}
2121
}
2222

2323
/// Adds a query to the [`ReadQuery`]
2424
pub fn add_query<S>(mut self, query: S) -> Self
2525
where
26-
S: ToString,
26+
S: Into<String>,
2727
{
28-
self.queries.push(query.to_string());
28+
self.queries.push(query.into());
2929
self
3030
}
3131
}

src/query/write_query.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use crate::query::{QueryType, ValidQuery};
66
use crate::{Error, Query, Timestamp};
7+
use std::fmt::{Display, Formatter};
78

89
// todo: batch write queries
910

@@ -19,12 +20,12 @@ impl WriteQuery {
1920
/// Creates a new [`WriteQuery`](crate::query::write_query::WriteQuery)
2021
pub fn new<S>(timestamp: Timestamp, measurement: S) -> Self
2122
where
22-
S: ToString,
23+
S: Into<String>,
2324
{
2425
WriteQuery {
2526
fields: vec![],
2627
tags: vec![],
27-
measurement: measurement.to_string(),
28+
measurement: measurement.into(),
2829
timestamp,
2930
}
3031
}
@@ -40,11 +41,11 @@ impl WriteQuery {
4041
/// ```
4142
pub fn add_field<S, I>(mut self, tag: S, value: I) -> Self
4243
where
43-
S: ToString,
44+
S: Into<String>,
4445
I: Into<Type>,
4546
{
4647
let val: Type = value.into();
47-
self.fields.push((tag.to_string(), val.to_string()));
48+
self.fields.push((tag.into(), val.to_string()));
4849
self
4950
}
5051

@@ -63,11 +64,11 @@ impl WriteQuery {
6364
/// ```
6465
pub fn add_tag<S, I>(mut self, tag: S, value: I) -> Self
6566
where
66-
S: ToString,
67+
S: Into<String>,
6768
I: Into<Type>,
6869
{
6970
let val: Type = value.into();
70-
self.tags.push((tag.to_string(), val.to_string()));
71+
self.tags.push((tag.into(), val.to_string()));
7172
self
7273
}
7374

@@ -93,16 +94,16 @@ pub enum Type {
9394
Text(String),
9495
}
9596

96-
impl ToString for Type {
97-
fn to_string(&self) -> String {
97+
impl Display for Type {
98+
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
9899
use Type::*;
99100

100101
match self {
101-
Boolean(x) => x.to_string(),
102-
Float(x) => x.to_string(),
103-
SignedInteger(x) => x.to_string(),
104-
UnsignedInteger(x) => x.to_string(),
105-
Text(text) => format!("\"{text}\"", text = text),
102+
Boolean(x) => write!(f, "{}", x),
103+
Float(x) => write!(f, "{}", x),
104+
SignedInteger(x) => write!(f, "{}", x),
105+
UnsignedInteger(x) => write!(f, "{}", x),
106+
Text(text) => write!(f, "\"{text}\"", text = text),
106107
}
107108
}
108109
}

tests/integration_tests.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn get_runtime() -> Runtime {
2020

2121
fn create_client<T>(db_name: T) -> Client
2222
where
23-
T: ToString,
23+
T: Into<String>,
2424
{
2525
Client::new("http://localhost:8086", db_name)
2626
}
@@ -35,19 +35,21 @@ impl Drop for RunOnDrop {
3535
}
3636
}
3737

38-
fn create_db<T>(test_name: T) -> Result<String, Error>
38+
fn create_db<T>(name: T) -> Result<String, Error>
3939
where
40-
T: ToString,
40+
T: Into<String>,
4141
{
42-
let query = format!("CREATE DATABASE {}", test_name.to_string());
42+
let test_name = name.into();
43+
let query = format!("CREATE DATABASE {}", test_name);
4344
get_runtime().block_on(create_client(test_name).query(&Query::raw_read_query(query)))
4445
}
4546

46-
fn delete_db<T>(test_name: T) -> Result<String, Error>
47+
fn delete_db<T>(name: T) -> Result<String, Error>
4748
where
48-
T: ToString,
49+
T: Into<String>,
4950
{
50-
let query = format!("DROP DATABASE {}", test_name.to_string());
51+
let test_name = name.into();
52+
let query = format!("DROP DATABASE {}", test_name);
5153
get_runtime().block_on(create_client(test_name).query(&Query::raw_read_query(query)))
5254
}
5355

0 commit comments

Comments
 (0)