Add Connect::map_addr() helper method (#326)

This commit is contained in:
Nikolay Kim 2024-03-29 21:48:11 +01:00 committed by GitHub
parent 18d3516343
commit baca449ba4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 41 additions and 3 deletions

View file

@ -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

View file

@ -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"]

View file

@ -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();

View file

@ -64,11 +64,11 @@ ntex-service = "2.0.1"
ntex-macros = "0.1.3"
ntex-util = "1.0.1"
ntex-bytes = "0.1.24"
ntex-server = "1.0.0"
ntex-server = "1.0.3"
ntex-h2 = "0.5.2"
ntex-rt = "0.4.12"
ntex-io = "1.0.1"
ntex-net = "1.0.0"
ntex-net = "1.0.1"
ntex-tls = "1.1.0"
base64 = "0.22"