feat(client): Implement TryFrom for Destination (#1810)

Add TryFrom<Uri> impl for Destination, for compiler version >= 1.34.

Closes #1808
This commit is contained in:
Andy Moran
2019-05-08 20:39:47 -05:00
committed by Sean McArthur
parent b342c38f08
commit d1183a8027
2 changed files with 32 additions and 2 deletions

View File

@@ -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");
}
}

View File

@@ -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<Uri> for Destination {
type Error = ::error::Error;
fn try_from(uri: Uri) -> Result<Self, Self::Error> {
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);