Skip to content

Commit 988d354

Browse files
authored
Don't force passing references to Client::query (#99)
1 parent b38a5e6 commit 988d354

File tree

6 files changed

+52
-32
lines changed

6 files changed

+52
-32
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ async fn main() {
8383
};
8484

8585
let write_result = client
86-
.query(&weather_reading.into_query("weather"))
86+
.query(weather_reading.into_query("weather"))
8787
.await;
8888
assert!(write_result.is_ok(), "Write result was not okay");
8989

9090
// Let's see if the data we wrote is there
9191
let read_query = Query::raw_read_query("SELECT * FROM weather");
9292

93-
let read_result = client.query(&read_query).await;
93+
let read_result = client.query(read_query).await;
9494
assert!(read_result.is_ok(), "Read result was not ok");
9595
println!("{}", read_result.unwrap());
9696
}

influxdb/src/client/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl Client {
170170
/// let query = Timestamp::Milliseconds(since_the_epoch)
171171
/// .into_query("weather")
172172
/// .add_field("temperature", 82);
173-
/// let results = client.query(&query).await?;
173+
/// let results = client.query(query).await?;
174174
///
175175
/// # Ok(())
176176
/// # }
@@ -181,7 +181,7 @@ impl Client {
181181
/// a [`Error`] variant will be returned.
182182
///
183183
/// [`Error`]: enum.Error.html
184-
pub async fn query<'q, Q>(&self, q: &'q Q) -> Result<String, Error>
184+
pub async fn query<Q>(&self, q: Q) -> Result<String, Error>
185185
where
186186
Q: Query,
187187
{

influxdb/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
//! };
5252
//!
5353
//! let write_result = client
54-
//! .query(&weather_reading.into_query("weather"))
54+
//! .query(weather_reading.into_query("weather"))
5555
//! .await;
5656
//! assert!(write_result.is_ok(), "Write result was not okay");
5757
//!
5858
//! // Let's see if the data we wrote is there
5959
//! let read_query = Query::raw_read_query("SELECT * FROM weather");
6060
//!
61-
//! let read_result = client.query(&read_query).await;
61+
//! let read_result = client.query(read_query).await;
6262
//! assert!(read_result.is_ok(), "Read result was not ok");
6363
//! println!("{}", read_result.unwrap());
6464
//! }

influxdb/src/query/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ pub trait Query {
115115
fn get_type(&self) -> QueryType;
116116
}
117117

118+
impl<Q: Query> Query for &Q {
119+
fn build(&self) -> Result<ValidQuery, Error> {
120+
Q::build(self)
121+
}
122+
123+
fn get_type(&self) -> QueryType {
124+
Q::get_type(self)
125+
}
126+
}
127+
128+
impl<Q: Query> Query for Box<Q> {
129+
fn build(&self) -> Result<ValidQuery, Error> {
130+
Q::build(&*self)
131+
}
132+
133+
fn get_type(&self) -> QueryType {
134+
Q::get_type(&*self)
135+
}
136+
}
137+
118138
pub trait InfluxDbWriteable {
119139
fn into_query<I: Into<String>>(self, name: I) -> WriteQuery;
120140
}

influxdb/tests/integration_tests.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async fn test_connection_error() {
5353
let client =
5454
Client::new("http://127.0.0.1:10086", test_name).with_auth("nopriv_user", "password");
5555
let read_query = ReadQuery::new("SELECT * FROM weather");
56-
let read_result = client.query(&read_query).await;
56+
let read_result = client.query(read_query).await;
5757
assert_result_err(&read_result);
5858
match read_result {
5959
Err(Error::ConnectionError { .. }) => {}
@@ -78,7 +78,7 @@ async fn test_authed_write_and_read() {
7878
Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password");
7979
let query = format!("CREATE DATABASE {}", TEST_NAME);
8080
client
81-
.query(&ReadQuery::new(query))
81+
.query(ReadQuery::new(query))
8282
.await
8383
.expect("could not setup db");
8484

@@ -87,11 +87,11 @@ async fn test_authed_write_and_read() {
8787
let write_query = Timestamp::Hours(11)
8888
.into_query("weather")
8989
.add_field("temperature", 82);
90-
let write_result = client.query(&write_query).await;
90+
let write_result = client.query(write_query).await;
9191
assert_result_ok(&write_result);
9292

9393
let read_query = ReadQuery::new("SELECT * FROM weather");
94-
let read_result = client.query(&read_query).await;
94+
let read_result = client.query(read_query).await;
9595
assert_result_ok(&read_result);
9696
assert!(
9797
!read_result.unwrap().contains("error"),
@@ -104,7 +104,7 @@ async fn test_authed_write_and_read() {
104104
let query = format!("DROP DATABASE {}", TEST_NAME);
105105

106106
client
107-
.query(&ReadQuery::new(query))
107+
.query(ReadQuery::new(query))
108108
.await
109109
.expect("could not clean up db");
110110
},
@@ -126,7 +126,7 @@ async fn test_wrong_authed_write_and_read() {
126126
Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password");
127127
let query = format!("CREATE DATABASE {}", TEST_NAME);
128128
client
129-
.query(&ReadQuery::new(query))
129+
.query(ReadQuery::new(query))
130130
.await
131131
.expect("could not setup db");
132132

@@ -135,7 +135,7 @@ async fn test_wrong_authed_write_and_read() {
135135
let write_query = Timestamp::Hours(11)
136136
.into_query("weather")
137137
.add_field("temperature", 82);
138-
let write_result = client.query(&write_query).await;
138+
let write_result = client.query(write_query).await;
139139
assert_result_err(&write_result);
140140
match write_result {
141141
Err(Error::AuthorizationError) => {}
@@ -146,7 +146,7 @@ async fn test_wrong_authed_write_and_read() {
146146
}
147147

148148
let read_query = ReadQuery::new("SELECT * FROM weather");
149-
let read_result = client.query(&read_query).await;
149+
let read_result = client.query(read_query).await;
150150
assert_result_err(&read_result);
151151
match read_result {
152152
Err(Error::AuthorizationError) => {}
@@ -159,7 +159,7 @@ async fn test_wrong_authed_write_and_read() {
159159
let client = Client::new("http://127.0.0.1:9086", TEST_NAME)
160160
.with_auth("nopriv_user", "password");
161161
let read_query = ReadQuery::new("SELECT * FROM weather");
162-
let read_result = client.query(&read_query).await;
162+
let read_result = client.query(read_query).await;
163163
assert_result_err(&read_result);
164164
match read_result {
165165
Err(Error::AuthenticationError) => {}
@@ -174,7 +174,7 @@ async fn test_wrong_authed_write_and_read() {
174174
Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password");
175175
let query = format!("DROP DATABASE {}", TEST_NAME);
176176
client
177-
.query(&ReadQuery::new(query))
177+
.query(ReadQuery::new(query))
178178
.await
179179
.expect("could not clean up db");
180180
},
@@ -196,14 +196,14 @@ async fn test_non_authed_write_and_read() {
196196
Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password");
197197
let query = format!("CREATE DATABASE {}", TEST_NAME);
198198
client
199-
.query(&ReadQuery::new(query))
199+
.query(ReadQuery::new(query))
200200
.await
201201
.expect("could not setup db");
202202
let non_authed_client = Client::new("http://127.0.0.1:9086", TEST_NAME);
203203
let write_query = Timestamp::Hours(11)
204204
.into_query("weather")
205205
.add_field("temperature", 82);
206-
let write_result = non_authed_client.query(&write_query).await;
206+
let write_result = non_authed_client.query(write_query).await;
207207
assert_result_err(&write_result);
208208
match write_result {
209209
Err(Error::AuthorizationError) => {}
@@ -214,7 +214,7 @@ async fn test_non_authed_write_and_read() {
214214
}
215215

216216
let read_query = ReadQuery::new("SELECT * FROM weather");
217-
let read_result = non_authed_client.query(&read_query).await;
217+
let read_result = non_authed_client.query(read_query).await;
218218
assert_result_err(&read_result);
219219
match read_result {
220220
Err(Error::AuthorizationError) => {}
@@ -229,7 +229,7 @@ async fn test_non_authed_write_and_read() {
229229
Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password");
230230
let query = format!("DROP DATABASE {}", TEST_NAME);
231231
client
232-
.query(&ReadQuery::new(query))
232+
.query(ReadQuery::new(query))
233233
.await
234234
.expect("could not clean up db");
235235
},
@@ -252,11 +252,11 @@ async fn test_write_and_read_field() {
252252
let write_query = Timestamp::Hours(11)
253253
.into_query("weather")
254254
.add_field("temperature", 82);
255-
let write_result = client.query(&write_query).await;
255+
let write_result = client.query(write_query).await;
256256
assert_result_ok(&write_result);
257257

258258
let read_query = ReadQuery::new("SELECT * FROM weather");
259-
let read_result = client.query(&read_query).await;
259+
let read_result = client.query(read_query).await;
260260
assert_result_ok(&read_result);
261261
assert!(
262262
!read_result.unwrap().contains("error"),
@@ -292,7 +292,7 @@ async fn test_write_and_read_option() {
292292
.into_query("weather")
293293
.add_field("temperature", 82)
294294
.add_field("wind_strength", <Option<u64>>::None);
295-
let write_result = client.query(&write_query).await;
295+
let write_result = client.query(write_query).await;
296296
assert_result_ok(&write_result);
297297

298298
#[derive(Deserialize, Debug, PartialEq)]
@@ -351,7 +351,7 @@ async fn test_json_query() {
351351
let write_query = Timestamp::Hours(11)
352352
.into_query("weather")
353353
.add_field("temperature", 82);
354-
let write_result = client.query(&write_query).await;
354+
let write_result = client.query(write_query).await;
355355
assert_result_ok(&write_result);
356356

357357
#[derive(Deserialize, Debug, PartialEq)]
@@ -404,7 +404,7 @@ async fn test_json_query_tagged() {
404404
.into_query("weather")
405405
.add_tag("location", "London")
406406
.add_field("temperature", 82);
407-
let write_result = client.query(&write_query).await;
407+
let write_result = client.query(write_query).await;
408408
assert_result_ok(&write_result);
409409

410410
#[derive(Deserialize, Debug, PartialEq)]
@@ -476,9 +476,9 @@ async fn test_json_query_vec() {
476476
.into_query("temperature_vec")
477477
.add_field("temperature", 18);
478478

479-
let _write_result = client.query(&write_query1).await;
480-
let _write_result2 = client.query(&write_query2).await;
481-
let _write_result2 = client.query(&write_query3).await;
479+
let _write_result = client.query(write_query1).await;
480+
let _write_result2 = client.query(write_query2).await;
481+
let _write_result2 = client.query(write_query3).await;
482482

483483
#[derive(Deserialize, Debug, PartialEq)]
484484
struct Weather {
@@ -536,8 +536,8 @@ async fn test_serde_multi_query() {
536536
.into_query("humidity")
537537
.add_field("humidity", 69);
538538

539-
let write_result = client.query(&write_query).await;
540-
let write_result2 = client.query(&write_query2).await;
539+
let write_result = client.query(write_query).await;
540+
let write_result2 = client.query(write_query2).await;
541541
assert_result_ok(&write_result);
542542
assert_result_ok(&write_result2);
543543

influxdb/tests/utilities.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ where
2828
{
2929
let test_name = name.into();
3030
let query = format!("CREATE DATABASE {}", test_name);
31-
create_client(test_name).query(&ReadQuery::new(query)).await
31+
create_client(test_name).query(ReadQuery::new(query)).await
3232
}
3333

3434
#[cfg(not(tarpaulin_include))]
@@ -38,7 +38,7 @@ where
3838
{
3939
let test_name = name.into();
4040
let query = format!("DROP DATABASE {}", test_name);
41-
create_client(test_name).query(&ReadQuery::new(query)).await
41+
create_client(test_name).query(ReadQuery::new(query)).await
4242
}
4343

4444
#[cfg(not(tarpaulin_include))]

0 commit comments

Comments
 (0)