Skip to content

Commit 6fc8234

Browse files
authored
Merge pull request #14 from lfittl/postgres-0.17
Update crate for rust-postgres 0.17 & Rust 2018 syntax
2 parents 3e66a77 + 86d15d4 commit 6fc8234

File tree

5 files changed

+88
-107
lines changed

5 files changed

+88
-107
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ language: rust
22
rust:
33
- nightly
44
- beta
5-
- 1.26.0
5+
- 1.40.0
66
addons:
77
postgresql: 9.4
88
script:
99
- cargo test
10-
- cargo test --features "with-time with-chrono"
10+
- cargo test --features "with-chrono-0_4"

Cargo.toml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22
name = "postgres_range"
33
version = "0.9.1"
44
authors = ["Steven Fackler <sfackler@gmail.com>"]
5+
edition = "2018"
56
license = "MIT"
67
description = "Range support for rust-postgres"
78
repository = "https://github.com/sfackler/rust-postgres-range"
89
documentation = "https://sfackler.github.io/rust-postgres-range/doc/v0.9.0/postgres_range"
910

1011
[features]
11-
with-time = ["time", "postgres-shared/with-time"]
12-
with-chrono = ["chrono", "postgres-shared/with-chrono"]
12+
with-chrono-0_4 = ["chrono-04", "postgres-types/with-chrono-0_4"]
1313

1414
[dependencies]
15-
time = { version = "0.1", optional = true }
16-
postgres-protocol = "0.3"
17-
chrono = { version = "0.4.0", optional = true }
18-
postgres-shared = "0.4.0"
15+
postgres-protocol = "0.5"
16+
postgres-types = "0.1"
17+
chrono-04 = { version = "0.4", package = "chrono", optional = true }
1918

2019
[dev-dependencies]
21-
postgres = { version = "0.15", features = ["with-time"] }
20+
postgres = { version = "0.17" }

src/chrono_04.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use chrono_04::{DateTime, NaiveDateTime, TimeZone};
2+
3+
use crate::{Normalizable, RangeBound, BoundSided};
4+
5+
impl<T> Normalizable for DateTime<T>
6+
where T: TimeZone {
7+
fn normalize<S>(bound: RangeBound<S, DateTime<T>>) -> RangeBound<S, DateTime<T>>
8+
where
9+
S: BoundSided,
10+
{
11+
bound
12+
}
13+
}
14+
15+
impl Normalizable for NaiveDateTime
16+
{
17+
fn normalize<S>(bound: RangeBound<S, NaiveDateTime>) -> RangeBound<S, NaiveDateTime>
18+
where
19+
S: BoundSided,
20+
{
21+
bound
22+
}
23+
}

src/impls.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use std::error::Error;
2-
use postgres_shared::types::{FromSql, IsNull, Kind, ToSql, Type};
2+
use postgres_types::{FromSql, IsNull, Kind, ToSql, Type};
3+
use postgres_types::private::BytesMut;
34
use postgres_protocol::{self as protocol, types};
45

5-
use {BoundSided, BoundType, Normalizable, Range, RangeBound};
6+
use crate::{BoundSided, BoundType, Normalizable, Range, RangeBound};
67

7-
impl<T> FromSql for Range<T>
8+
impl<'a, T> FromSql<'a> for Range<T>
89
where
9-
T: PartialOrd + Normalizable + FromSql,
10+
T: PartialOrd + Normalizable + FromSql<'a>,
1011
{
11-
fn from_sql(ty: &Type, raw: &[u8]) -> Result<Range<T>, Box<Error + Sync + Send>> {
12-
let element_type = match ty.kind() {
13-
&Kind::Range(ref ty) => ty,
12+
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Range<T>, Box<dyn Error + Sync + Send>> {
13+
let element_type = match *ty.kind() {
14+
Kind::Range(ref ty) => ty,
1415
_ => panic!("unexpected type {:?}", ty),
1516
};
1617

@@ -25,16 +26,16 @@ where
2526
}
2627

2728
fn accepts(ty: &Type) -> bool {
28-
match ty.kind() {
29-
&Kind::Range(ref inner) => <T as FromSql>::accepts(inner),
29+
match *ty.kind() {
30+
Kind::Range(ref inner) => <T as FromSql>::accepts(inner),
3031
_ => false,
3132
}
3233
}
3334
}
3435

35-
fn bound_from_sql<T, S>(bound: types::RangeBound<Option<&[u8]>>, ty: &Type) -> Result<Option<RangeBound<S, T>>, Box<Error + Sync + Send>>
36+
fn bound_from_sql<'a, T, S>(bound: types::RangeBound<Option<&'a [u8]>>, ty: &Type) -> Result<Option<RangeBound<S, T>>, Box<dyn Error + Sync + Send>>
3637
where
37-
T: PartialOrd + Normalizable + FromSql,
38+
T: PartialOrd + Normalizable + FromSql<'a>,
3839
S: BoundSided,
3940
{
4041
match bound {
@@ -60,9 +61,9 @@ impl<T> ToSql for Range<T>
6061
where
6162
T: PartialOrd + Normalizable + ToSql,
6263
{
63-
fn to_sql(&self, ty: &Type, buf: &mut Vec<u8>) -> Result<IsNull, Box<Error + Sync + Send>> {
64-
let element_type = match ty.kind() {
65-
&Kind::Range(ref ty) => ty,
64+
fn to_sql(&self, ty: &Type, buf: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
65+
let element_type = match *ty.kind() {
66+
Kind::Range(ref ty) => ty,
6667
_ => panic!("unexpected type {:?}", ty),
6768
};
6869

@@ -80,16 +81,16 @@ where
8081
}
8182

8283
fn accepts(ty: &Type) -> bool {
83-
match ty.kind() {
84-
&Kind::Range(ref inner) => <T as ToSql>::accepts(inner),
84+
match *ty.kind() {
85+
Kind::Range(ref inner) => <T as ToSql>::accepts(inner),
8586
_ => false,
8687
}
8788
}
8889

8990
to_sql_checked!();
9091
}
9192

92-
fn bound_to_sql<S, T>(bound: Option<&RangeBound<S, T>>, ty: &Type, buf: &mut Vec<u8>) -> Result<types::RangeBound<protocol::IsNull>, Box<Error + Sync + Send>>
93+
fn bound_to_sql<S, T>(bound: Option<&RangeBound<S, T>>, ty: &Type, buf: &mut BytesMut) -> Result<types::RangeBound<protocol::IsNull>, Box<dyn Error + Sync + Send>>
9394
where
9495
S: BoundSided,
9596
T: ToSql,
@@ -114,10 +115,10 @@ where
114115
mod test {
115116
use std::fmt;
116117

117-
use postgres::{Connection, TlsMode};
118+
use postgres::{Client, NoTls};
118119
use postgres::types::{FromSql, ToSql};
119-
#[cfg(feature = "with-time")]
120-
use time::{self, Timespec};
120+
#[cfg(feature = "with-chrono-0_4")]
121+
use chrono_04::{TimeZone, Utc, Duration};
121122

122123
macro_rules! test_range {
123124
($name:expr, $t:ty, $low:expr, $low_str:expr, $high:expr, $high_str:expr) => ({
@@ -140,16 +141,21 @@ mod test {
140141
})
141142
}
142143

143-
fn test_type<T: PartialEq + FromSql + ToSql, S: fmt::Display>(sql_type: &str, checks: &[(T, S)]) {
144-
let conn = Connection::connect("postgres://postgres@localhost", TlsMode::None).unwrap();
144+
145+
fn test_type<T, S>(sql_type: &str, checks: &[(T, S)])
146+
where for<'a>
147+
T: Sync + PartialEq + FromSql<'a> + ToSql,
148+
S: fmt::Display
149+
{
150+
let mut conn = Client::connect("postgres://postgres@localhost", NoTls).unwrap();
145151
for &(ref val, ref repr) in checks {
146152
let stmt = conn.prepare(&*format!("SELECT {}::{}", *repr, sql_type))
147153
.unwrap();
148-
let result = stmt.query(&[]).unwrap().iter().next().unwrap().get(0);
154+
let result = conn.query(&stmt, &[]).unwrap().iter().next().unwrap().get(0);
149155
assert!(val == &result);
150156

151157
let stmt = conn.prepare(&*format!("SELECT $1::{}", sql_type)).unwrap();
152-
let result = stmt.query(&[val]).unwrap().iter().next().unwrap().get(0);
158+
let result = conn.query(&stmt, &[val]).unwrap().iter().next().unwrap().get(0);
153159
assert!(val == &result);
154160
}
155161
}
@@ -164,25 +170,19 @@ mod test {
164170
test_range!("INT8RANGE", i64, 100i64, "100", 200i64, "200")
165171
}
166172

167-
#[cfg(feature = "with-time")]
168-
fn test_timespec_range_params(sql_type: &str) {
169-
fn t(time: &str) -> Timespec {
170-
time::strptime(time, "%Y-%m-%d").unwrap().to_timespec()
171-
}
172-
let low = "1970-01-01";
173-
let high = "1980-01-01";
174-
test_range!(sql_type, Timespec, t(low), low, t(high), high);
175-
}
176-
177173
#[test]
178-
#[cfg(feature = "with-time")]
174+
#[cfg(feature = "with-chrono-0_4")]
179175
fn test_tsrange_params() {
180-
test_timespec_range_params("TSRANGE");
176+
let low = Utc.timestamp(0, 0);
177+
let high = low + Duration::days(10);
178+
test_range!("TSRANGE", NaiveDateTime, low.naive_utc(), "1970-01-01", high.naive_utc(), "1970-01-11");
181179
}
182180

183181
#[test]
184-
#[cfg(feature = "with-time")]
182+
#[cfg(feature = "with-chrono-0_4")]
185183
fn test_tstzrange_params() {
186-
test_timespec_range_params("TSTZRANGE");
184+
let low = Utc.timestamp(0, 0);
185+
let high = low + Duration::days(10);
186+
test_range!("TSTZRANGE", DateTime<Utc>, low, "1970-01-01", high, "1970-01-11");
187187
}
188188
}

src/lib.rs

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
//! Types dealing with ranges of values
22
#![doc(html_root_url = "https://sfackler.github.io/rust-postgres-range/doc/v0.9")]
3+
#![warn(clippy::all, rust_2018_idioms, missing_docs)]
34

4-
extern crate postgres_protocol;
55
#[macro_use(to_sql_checked)]
6-
extern crate postgres_shared;
6+
extern crate postgres_types;
77

8-
#[cfg(feature = "with-time")]
9-
extern crate time;
10-
#[cfg(feature = "with-chrono")]
11-
extern crate chrono;
8+
#[cfg(feature = "with-chrono-0_4")]
9+
mod chrono_04;
1210

13-
#[cfg(test)]
14-
extern crate postgres;
15-
16-
#[cfg(feature = "with-chrono")]
17-
use chrono::{DateTime, NaiveDateTime, TimeZone};
1811
use std::cmp::Ordering;
1912
use std::fmt;
2013
use std::i32;
2114
use std::i64;
2215
use std::marker::PhantomData;
23-
#[cfg(feature = "with-time")]
24-
use time::Timespec;
2516

2617
use BoundSide::{Lower, Upper};
2718
use BoundType::{Exclusive, Inclusive};
@@ -155,38 +146,6 @@ macro_rules! bounded_normalizable {
155146
bounded_normalizable!(i32);
156147
bounded_normalizable!(i64);
157148

158-
#[cfg(feature = "with-time")]
159-
impl Normalizable for Timespec {
160-
fn normalize<S>(bound: RangeBound<S, Timespec>) -> RangeBound<S, Timespec>
161-
where
162-
S: BoundSided,
163-
{
164-
bound
165-
}
166-
}
167-
168-
#[cfg(feature = "with-chrono")]
169-
impl<T> Normalizable for DateTime<T>
170-
where T: TimeZone {
171-
fn normalize<S>(bound: RangeBound<S, DateTime<T>>) -> RangeBound<S, DateTime<T>>
172-
where
173-
S: BoundSided,
174-
{
175-
bound
176-
}
177-
}
178-
179-
#[cfg(feature = "with-chrono")]
180-
impl Normalizable for NaiveDateTime
181-
{
182-
fn normalize<S>(bound: RangeBound<S, NaiveDateTime>) -> RangeBound<S, NaiveDateTime>
183-
where
184-
S: BoundSided,
185-
{
186-
bound
187-
}
188-
}
189-
190149
/// The possible sides of a bound.
191150
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
192151
pub enum BoundSide {
@@ -260,7 +219,7 @@ where
260219
fn clone(&self) -> RangeBound<S, T> {
261220
RangeBound {
262221
value: self.value.clone(),
263-
type_: self.type_.clone(),
222+
type_: self.type_,
264223
_m: PhantomData,
265224
}
266225
}
@@ -271,7 +230,7 @@ where
271230
S: BoundSided,
272231
T: fmt::Debug,
273232
{
274-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
233+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
275234
fmt.debug_struct("RangeBound")
276235
.field("value", &self.value)
277236
.field("type_", &self.type_)
@@ -284,7 +243,7 @@ where
284243
S: BoundSided,
285244
T: fmt::Display,
286245
{
287-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
246+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
288247
let (lower, upper) = match self.type_ {
289248
Inclusive => ('[', ']'),
290249
Exclusive => ('(', ')'),
@@ -306,9 +265,9 @@ where
306265
self.value == other.value && self.type_ == other.type_
307266
}
308267

309-
fn ne(&self, other: &RangeBound<S, T>) -> bool {
268+
/*fn ne(&self, other: &RangeBound<S, T>) -> bool {
310269
self.value != other.value || self.type_ != other.type_
311-
}
270+
}*/
312271
}
313272

314273
impl<S, T> Eq for RangeBound<S, T>
@@ -364,8 +323,8 @@ where
364323
/// Constructs a new range bound
365324
pub fn new(value: T, type_: BoundType) -> RangeBound<S, T> {
366325
RangeBound {
367-
value: value,
368-
type_: type_,
326+
value,
327+
type_,
369328
_m: PhantomData,
370329
}
371330
}
@@ -381,7 +340,7 @@ where
381340
}
382341
}
383342

384-
struct OptBound<'a, S: 'a + BoundSided, T: 'a>(Option<&'a RangeBound<S, T>>);
343+
struct OptBound<'a, S: BoundSided, T>(Option<&'a RangeBound<S, T>>);
385344

386345
impl<'a, S, T> PartialEq for OptBound<'a, S, T>
387346
where
@@ -393,10 +352,10 @@ where
393352
self_ == other
394353
}
395354

396-
fn ne(&self, &OptBound(ref other): &OptBound<'a, S, T>) -> bool {
355+
/*fn ne(&self, &OptBound(ref other): &OptBound<'a, S, T>) -> bool {
397356
let &OptBound(ref self_) = self;
398357
self_ != other
399-
}
358+
}*/
400359
}
401360

402361
impl<'a, S, T> PartialOrd for OptBound<'a, S, T>
@@ -433,7 +392,7 @@ impl<T> fmt::Display for Range<T>
433392
where
434393
T: fmt::Display,
435394
{
436-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
395+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
437396
match self.inner {
438397
Empty => write!(fmt, "empty"),
439398
Normal(ref lower, ref upper) => {
@@ -558,7 +517,7 @@ where
558517
let (_, OptBound(lower)) = order(OptBound(self.lower()), OptBound(other.lower()));
559518
let (OptBound(upper), _) = order(OptBound(self.upper()), OptBound(other.upper()));
560519

561-
Range::new(lower.map(|v| v.clone()), upper.map(|v| v.clone()))
520+
Range::new(lower.cloned(), upper.cloned())
562521
}
563522

564523
/// Returns the union of this range with another if it is contiguous.
@@ -595,8 +554,8 @@ where
595554
None
596555
} else {
597556
Some(Range::new(
598-
l_lower.map(|v| v.clone()),
599-
u_upper.map(|v| v.clone()),
557+
l_lower.cloned(),
558+
u_upper.cloned(),
600559
))
601560
}
602561
}
@@ -758,7 +717,7 @@ mod test {
758717
assert_eq!(r1, (range!('(',; ')')).intersect(&r1));
759718

760719
let r2 = range!('(' 10i32,; ')');
761-
let exp = Range::new(r2.lower().map(|v| v.clone()), r1.upper().map(|v| v.clone()));
720+
let exp = Range::new(r2.lower().copied(), r1.upper().copied());
762721
assert_eq!(exp, r1.intersect(&r2));
763722
assert_eq!(exp, r2.intersect(&r1));
764723

0 commit comments

Comments
 (0)