Skip to content

Commit

Permalink
feat(client): Implement TryFrom for Destination (#1810)
Browse files Browse the repository at this point in the history
Add TryFrom<Uri> impl for Destination, for compiler version >= 1.34.

Closes #1808
  • Loading branch information
M3rs authored and seanmonstar committed May 9, 2019
1 parent b342c38 commit d1183a8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
28 changes: 27 additions & 1 deletion src/client/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -381,7 +391,7 @@ where

#[cfg(test)]
mod tests {
use super::{Connected, Destination};
use super::{Connected, Destination, TryFrom};

#[test]
fn test_destination_set_scheme() {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit d1183a8

Please sign in to comment.