mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-04 21:37:58 +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
|
# Changes
|
||||||
|
|
||||||
|
## [1.0.1] - 2024-03-29
|
||||||
|
|
||||||
|
* Add Connect::map_addr() helper method
|
||||||
|
|
||||||
|
* Add `Address` support for ByteString
|
||||||
|
|
||||||
## [1.0.0] - 2024-03-25
|
## [1.0.0] - 2024-03-25
|
||||||
|
|
||||||
* Move to separate crate
|
* Move to separate crate
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ntex-net"
|
name = "ntex-net"
|
||||||
version = "1.0.0"
|
version = "1.0.1"
|
||||||
authors = ["ntex contributors <team@ntex.rs>"]
|
authors = ["ntex contributors <team@ntex.rs>"]
|
||||||
description = "ntexwork utils for ntex framework"
|
description = "ntexwork utils for ntex framework"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::collections::{vec_deque, VecDeque};
|
use std::collections::{vec_deque, VecDeque};
|
||||||
use std::{fmt, iter::FusedIterator, net::SocketAddr};
|
use std::{fmt, iter::FusedIterator, net::SocketAddr};
|
||||||
|
|
||||||
|
use ntex_bytes::ByteString;
|
||||||
use ntex_util::future::Either;
|
use ntex_util::future::Either;
|
||||||
|
|
||||||
/// Connect request
|
/// 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 {
|
impl Address for &'static str {
|
||||||
fn host(&self) -> &str {
|
fn host(&self) -> &str {
|
||||||
self
|
self
|
||||||
|
@ -157,6 +168,20 @@ impl<T: Address> Connect<T> {
|
||||||
pub fn get_ref(&self) -> &T {
|
pub fn get_ref(&self) -> &T {
|
||||||
&self.req
|
&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> {
|
impl<T: Clone> Clone for Connect<T> {
|
||||||
|
@ -286,6 +311,13 @@ mod tests {
|
||||||
assert!(connect.addrs().next().is_none());
|
assert!(connect.addrs().next().is_none());
|
||||||
assert!(format!("{:?}", connect.clone()).contains("Connect"));
|
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();
|
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
|
||||||
connect = connect.set_addrs(vec![addr]);
|
connect = connect.set_addrs(vec![addr]);
|
||||||
let addrs = connect.addrs().clone();
|
let addrs = connect.addrs().clone();
|
||||||
|
|
|
@ -64,11 +64,11 @@ ntex-service = "2.0.1"
|
||||||
ntex-macros = "0.1.3"
|
ntex-macros = "0.1.3"
|
||||||
ntex-util = "1.0.1"
|
ntex-util = "1.0.1"
|
||||||
ntex-bytes = "0.1.24"
|
ntex-bytes = "0.1.24"
|
||||||
ntex-server = "1.0.0"
|
ntex-server = "1.0.3"
|
||||||
ntex-h2 = "0.5.2"
|
ntex-h2 = "0.5.2"
|
||||||
ntex-rt = "0.4.12"
|
ntex-rt = "0.4.12"
|
||||||
ntex-io = "1.0.1"
|
ntex-io = "1.0.1"
|
||||||
ntex-net = "1.0.0"
|
ntex-net = "1.0.1"
|
||||||
ntex-tls = "1.1.0"
|
ntex-tls = "1.1.0"
|
||||||
|
|
||||||
base64 = "0.22"
|
base64 = "0.22"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue