diff --git a/build.rs b/build.rs index c34535fe4d..aae8968807 100644 --- a/build.rs +++ b/build.rs @@ -3,7 +3,11 @@ extern crate rustc_version; use rustc_version::{version, Version}; fn main() { - if version().unwrap() >= Version::parse("1.30.0").unwrap() { + let version = version().unwrap(); + if version >= Version::parse("1.30.0").unwrap() { println!("cargo:rustc-cfg=error_source"); } + if version >= Version::parse("1.34.0").unwrap() { + println!("cargo:rustc-cfg=try_from"); + } } diff --git a/src/client/connect/mod.rs b/src/client/connect/mod.rs index 764835cfb2..ea04ffa09f 100644 --- a/src/client/connect/mod.rs +++ b/src/client/connect/mod.rs @@ -7,6 +7,7 @@ //! - The [`Connect`](Connect) trait and related types to build custom connectors. use std::error::Error as StdError; use std::{fmt, mem}; +#[cfg(try_from)] use std::convert::TryFrom; use bytes::{BufMut, Bytes, BytesMut}; use futures::Future; @@ -251,6 +252,15 @@ impl Destination { */ } +#[cfg(try_from)] +impl TryFrom for Destination { + type Error = ::error::Error; + + fn try_from(uri: Uri) -> Result { + Destination::try_from_uri(uri) + } +} + impl Connected { /// Create new `Connected` type with empty metadata. pub fn new() -> Connected { @@ -381,7 +391,7 @@ where #[cfg(test)] mod tests { - use super::{Connected, Destination}; + use super::{Connected, Destination, TryFrom}; #[test] fn test_destination_set_scheme() { @@ -527,6 +537,22 @@ mod tests { assert_eq!(dst.port(), None); } + #[cfg(try_from)] + #[test] + fn test_try_from_destination() { + let uri: http::Uri = "http://hyper.rs".parse().expect("initial parse"); + let result = Destination::try_from(uri); + assert_eq!(result.is_ok(), true); + } + + #[cfg(try_from)] + #[test] + fn test_try_from_no_scheme() { + let uri: http::Uri = "hyper.rs".parse().expect("initial parse error"); + let result = Destination::try_from(uri); + assert_eq!(result.is_err(), true); + } + #[derive(Clone, Debug, PartialEq)] struct Ex1(usize);