mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-03 21:07:39 +03:00
Add Connect::map_addr() helper method (#326)
This commit is contained in:
parent
18d3516343
commit
baca449ba4
4 changed files with 41 additions and 3 deletions
|
@ -1,5 +1,11 @@
|
|||
# Changes
|
||||
|
||||
## [1.0.1] - 2024-03-29
|
||||
|
||||
* Add Connect::map_addr() helper method
|
||||
|
||||
* Add `Address` support for ByteString
|
||||
|
||||
## [1.0.0] - 2024-03-25
|
||||
|
||||
* Move to separate crate
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ntex-net"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
authors = ["ntex contributors <team@ntex.rs>"]
|
||||
description = "ntexwork utils for ntex framework"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::collections::{vec_deque, VecDeque};
|
||||
use std::{fmt, iter::FusedIterator, net::SocketAddr};
|
||||
|
||||
use ntex_bytes::ByteString;
|
||||
use ntex_util::future::Either;
|
||||
|
||||
/// Connect request
|
||||
|
@ -27,6 +28,16 @@ impl Address for String {
|
|||
}
|
||||
}
|
||||
|
||||
impl Address for ByteString {
|
||||
fn host(&self) -> &str {
|
||||
self
|
||||
}
|
||||
|
||||
fn port(&self) -> Option<u16> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl Address for &'static str {
|
||||
fn host(&self) -> &str {
|
||||
self
|
||||
|
@ -157,6 +168,20 @@ impl<T: Address> Connect<T> {
|
|||
pub fn get_ref(&self) -> &T {
|
||||
&self.req
|
||||
}
|
||||
|
||||
/// Call callback with current address and construct new Connect instance.
|
||||
pub fn map_addr<F, R>(self, f: F) -> Connect<R>
|
||||
where
|
||||
F: FnOnce(T) -> R,
|
||||
{
|
||||
let req = f(self.req);
|
||||
|
||||
Connect {
|
||||
req,
|
||||
port: self.port,
|
||||
addr: self.addr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Clone for Connect<T> {
|
||||
|
@ -286,6 +311,13 @@ mod tests {
|
|||
assert!(connect.addrs().next().is_none());
|
||||
assert!(format!("{:?}", connect.clone()).contains("Connect"));
|
||||
|
||||
let c = connect.clone().map_addr(|_| "www.rust-lang.org:80");
|
||||
assert_eq!(c.host(), "www.rust-lang.org:80");
|
||||
assert_eq!(c.port(), 80);
|
||||
let addrs = c.addrs().clone();
|
||||
assert_eq!(format!("{:?}", addrs), "[]");
|
||||
assert!(c.addrs().next().is_none());
|
||||
|
||||
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
|
||||
connect = connect.set_addrs(vec![addr]);
|
||||
let addrs = connect.addrs().clone();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue