|
7 | 7 | //! - The [`Connect`](Connect) trait and related types to build custom connectors. |
8 | 8 | use std::error::Error as StdError; |
9 | 9 | use std::{fmt, mem}; |
| 10 | +#[cfg(try_from)] use std::convert::TryFrom; |
10 | 11 |
|
11 | 12 | use bytes::{BufMut, Bytes, BytesMut}; |
12 | 13 | use futures::Future; |
@@ -251,6 +252,15 @@ impl Destination { |
251 | 252 | */ |
252 | 253 | } |
253 | 254 |
|
| 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 | + |
254 | 264 | impl Connected { |
255 | 265 | /// Create new `Connected` type with empty metadata. |
256 | 266 | pub fn new() -> Connected { |
@@ -381,7 +391,7 @@ where |
381 | 391 |
|
382 | 392 | #[cfg(test)] |
383 | 393 | mod tests { |
384 | | - use super::{Connected, Destination}; |
| 394 | + use super::{Connected, Destination, TryFrom}; |
385 | 395 |
|
386 | 396 | #[test] |
387 | 397 | fn test_destination_set_scheme() { |
@@ -527,6 +537,22 @@ mod tests { |
527 | 537 | assert_eq!(dst.port(), None); |
528 | 538 | } |
529 | 539 |
|
| 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 | + |
530 | 556 | #[derive(Clone, Debug, PartialEq)] |
531 | 557 | struct Ex1(usize); |
532 | 558 |
|
|
0 commit comments