Skip to content

Commit d1183a8

Browse files
M3rsseanmonstar
authored andcommitted
feat(client): Implement TryFrom for Destination (#1810)
Add TryFrom<Uri> impl for Destination, for compiler version >= 1.34. Closes #1808
1 parent b342c38 commit d1183a8

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ extern crate rustc_version;
33
use rustc_version::{version, Version};
44

55
fn main() {
6-
if version().unwrap() >= Version::parse("1.30.0").unwrap() {
6+
let version = version().unwrap();
7+
if version >= Version::parse("1.30.0").unwrap() {
78
println!("cargo:rustc-cfg=error_source");
89
}
10+
if version >= Version::parse("1.34.0").unwrap() {
11+
println!("cargo:rustc-cfg=try_from");
12+
}
913
}

src/client/connect/mod.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! - The [`Connect`](Connect) trait and related types to build custom connectors.
88
use std::error::Error as StdError;
99
use std::{fmt, mem};
10+
#[cfg(try_from)] use std::convert::TryFrom;
1011

1112
use bytes::{BufMut, Bytes, BytesMut};
1213
use futures::Future;
@@ -251,6 +252,15 @@ impl Destination {
251252
*/
252253
}
253254

255+
#[cfg(try_from)]
256+
impl TryFrom<Uri> for Destination {
257+
type Error = ::error::Error;
258+
259+
fn try_from(uri: Uri) -> Result<Self, Self::Error> {
260+
Destination::try_from_uri(uri)
261+
}
262+
}
263+
254264
impl Connected {
255265
/// Create new `Connected` type with empty metadata.
256266
pub fn new() -> Connected {
@@ -381,7 +391,7 @@ where
381391

382392
#[cfg(test)]
383393
mod tests {
384-
use super::{Connected, Destination};
394+
use super::{Connected, Destination, TryFrom};
385395

386396
#[test]
387397
fn test_destination_set_scheme() {
@@ -527,6 +537,22 @@ mod tests {
527537
assert_eq!(dst.port(), None);
528538
}
529539

540+
#[cfg(try_from)]
541+
#[test]
542+
fn test_try_from_destination() {
543+
let uri: http::Uri = "http://hyper.rs".parse().expect("initial parse");
544+
let result = Destination::try_from(uri);
545+
assert_eq!(result.is_ok(), true);
546+
}
547+
548+
#[cfg(try_from)]
549+
#[test]
550+
fn test_try_from_no_scheme() {
551+
let uri: http::Uri = "hyper.rs".parse().expect("initial parse error");
552+
let result = Destination::try_from(uri);
553+
assert_eq!(result.is_err(), true);
554+
}
555+
530556
#[derive(Clone, Debug, PartialEq)]
531557
struct Ex1(usize);
532558

0 commit comments

Comments
 (0)