Skip to content

Commit b6aceb2

Browse files
authored
Remove the need for mut in some apis (#733)
1 parent 07536ca commit b6aceb2

31 files changed

+362
-255
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ ntex-macros = { path = "ntex-macros" }
4242
ntex-util = { path = "ntex-util" }
4343

4444
[workspace.dependencies]
45-
ntex = "3.0.0-pre.15"
45+
ntex = "3.0.0"
4646
ntex-bytes = "1.4.1"
4747
ntex-codec = "1.1.0"
48-
ntex-io = "3.6.0"
48+
ntex-io = "3.7.0"
4949
ntex-dispatcher = "3.0.0"
5050
ntex-net = "3.5.2"
5151
ntex-http = "1.0.0"

ntex-io/CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changes
22

3-
## [3.7.0] - 2026-01-xx
3+
## [3.7.0] - 2026-01-31
4+
5+
* Fix BufferCfg::resize_min()
46

57
* Drop Dispatcher
68

ntex-io/src/cfg.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::UnsafeCell, cmp};
1+
use std::cell::UnsafeCell;
22

33
use ntex_bytes::{BytesMut, buf::BufMut};
44
use ntex_service::cfg::{CfgContext, Configuration};
@@ -263,12 +263,17 @@ impl BufConfig {
263263
#[inline]
264264
/// Resize buffer
265265
pub fn resize_min(&self, buf: &mut BytesMut, size: usize) {
266-
if buf.remaining_mut() < cmp::min(self.low, size) {
267-
let cap = buf.capacity();
268-
let mut new_size = self.high;
269-
while cap >= new_size && size >= new_size {
266+
let mut avail = buf.remaining_mut();
267+
println!("resize: {avail} -- {size}");
268+
if avail < size {
269+
avail += self.high;
270+
let mut new_size = self.high + self.high;
271+
println!("resize 2: {avail} -- {new_size}");
272+
while avail < size {
273+
avail += self.high;
270274
new_size += self.high;
271275
}
276+
println!("resize 3: {new_size}");
272277
buf.reserve_capacity(new_size);
273278
}
274279
}

ntex-tls/examples/webclient.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async fn main() -> Result<(), SendRequestError> {
3030
.unwrap();
3131

3232
// Create request builder, configure request and send
33-
let mut response = client
33+
let response = client
3434
.get("https://127.0.0.1:8443/")
3535
.header("User-Agent", "ntex")
3636
.send()

ntex/CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changes
22

3+
## [3.0.0] 2026-02-xx
4+
5+
* Remove the need for mut in h2::Payload
6+
7+
* Remove the need for mut in ClientResponse::json() and ClientResponse::body()
8+
39
## [3.0.0-pre.15] 2026-01-29
410

511
* Use ntex_dispatcher::Dispatcher instead of ntex-io

ntex/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex"
3-
version = "3.0.0-pre.15"
3+
version = "3.0.0"
44
authors = ["ntex contributors <team@ntex.rs>"]
55
description = "Framework for composable network services"
66
readme = "README.md"

ntex/examples/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async fn main() -> Result<(), SendRequestError> {
77
let client = Client::new().await;
88

99
// Create request builder, configure request and send
10-
let mut response = client
10+
let response = client
1111
.get("https://www.rust-lang.org/")
1212
.header("User-Agent", "ntex")
1313
.send()

ntex/src/client/h1proto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ where
5454
);
5555

5656
// send request
57-
let codec = h1::ClientCodec::default();
57+
let codec = h1::ClientCodec::new(true, io.cfg());
5858
io.send((head, body.size()).into(), &codec).await?;
5959

6060
log::trace!("http1 request has been sent");

ntex/src/client/h2proto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ async fn get_response(
129129
stream.tag(),
130130
stream.id()
131131
);
132-
let (mut pl, payload) = H2Payload::create(stream.empty_capacity());
132+
let (pl, payload) = H2Payload::create(stream.empty_capacity());
133133

134134
let _ = crate::rt::spawn(async move {
135135
loop {

ntex/src/client/response.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::cell::{Ref, RefMut};
22
use std::task::{Context, Poll};
3-
use std::{fmt, future::Future, marker::PhantomData, mem, pin::Pin, rc::Rc};
3+
use std::{cell::Cell, fmt, future::Future, marker::PhantomData, pin::Pin, rc::Rc};
44

55
use serde::de::DeserializeOwned;
66

@@ -18,7 +18,7 @@ use super::{ClientConfig, error::JsonPayloadError};
1818
/// Client Response
1919
pub struct ClientResponse {
2020
pub(crate) head: ResponseHead,
21-
pub(crate) payload: Payload,
21+
pub(crate) payload: Cell<Option<Payload>>,
2222
config: Rc<ClientConfig>,
2323
}
2424

@@ -63,8 +63,8 @@ impl ClientResponse {
6363
pub fn new(head: ResponseHead, payload: Payload, config: Rc<ClientConfig>) -> Self {
6464
ClientResponse {
6565
head,
66-
payload,
6766
config,
67+
payload: Cell::new(Some(payload)),
6868
}
6969
}
7070

@@ -114,13 +114,17 @@ impl ClientResponse {
114114
}
115115

116116
/// Set a body and return previous body value
117-
pub fn set_payload(&mut self, payload: Payload) {
118-
self.payload = payload;
117+
pub fn set_payload(&self, payload: Payload) {
118+
self.payload.set(Some(payload));
119119
}
120120

121121
/// Get response's payload
122-
pub fn take_payload(&mut self) -> Payload {
123-
mem::take(&mut self.payload)
122+
pub fn take_payload(&self) -> Payload {
123+
if let Some(pl) = self.payload.take() {
124+
pl
125+
} else {
126+
Payload::None
127+
}
124128
}
125129

126130
/// Request extensions
@@ -138,7 +142,7 @@ impl ClientResponse {
138142

139143
impl ClientResponse {
140144
/// Loads http response's body.
141-
pub fn body(&mut self) -> MessageBody {
145+
pub fn body(&self) -> MessageBody {
142146
MessageBody::new(self)
143147
}
144148

@@ -149,7 +153,7 @@ impl ClientResponse {
149153
///
150154
/// * content type is not `application/json`
151155
/// * content length is greater than 256k
152-
pub fn json<T: DeserializeOwned>(&mut self) -> JsonBody<T> {
156+
pub fn json<T: DeserializeOwned>(&self) -> JsonBody<T> {
153157
JsonBody::new(self)
154158
}
155159
}
@@ -158,7 +162,13 @@ impl Stream for ClientResponse {
158162
type Item = Result<Bytes, PayloadError>;
159163

160164
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
161-
Pin::new(&mut self.get_mut().payload).poll_next(cx)
165+
if let Some(mut pl) = self.payload.take() {
166+
let result = Pin::new(&mut pl).poll_next(cx);
167+
self.payload.set(Some(pl));
168+
result
169+
} else {
170+
Poll::Ready(None)
171+
}
162172
}
163173
}
164174

@@ -183,7 +193,7 @@ pub struct MessageBody {
183193

184194
impl MessageBody {
185195
/// Create `MessageBody` for request.
186-
pub fn new(res: &mut ClientResponse) -> MessageBody {
196+
pub fn new(res: &ClientResponse) -> MessageBody {
187197
let mut len = None;
188198
if let Some(l) = res.headers().get(&CONTENT_LENGTH) {
189199
if let Ok(s) = l.to_str() {
@@ -276,7 +286,7 @@ where
276286
U: DeserializeOwned,
277287
{
278288
/// Create `JsonBody` for request.
279-
pub fn new(res: &mut ClientResponse) -> Self {
289+
pub fn new(res: &ClientResponse) -> Self {
280290
// check content-type
281291
let json = if let Ok(Some(mime)) = res.mime_type() {
282292
mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON)
@@ -423,24 +433,24 @@ mod tests {
423433

424434
#[crate::rt_test]
425435
async fn test_body() {
426-
let mut req = TestResponse::with_header(header::CONTENT_LENGTH, "xxxx").finish();
436+
let req = TestResponse::with_header(header::CONTENT_LENGTH, "xxxx").finish();
427437
match req.body().await.err().unwrap() {
428438
PayloadError::UnknownLength => (),
429439
_ => unreachable!("error"),
430440
}
431441

432-
let mut req = TestResponse::with_header(header::CONTENT_LENGTH, "1000000").finish();
442+
let req = TestResponse::with_header(header::CONTENT_LENGTH, "1000000").finish();
433443
match req.body().await.err().unwrap() {
434444
PayloadError::Overflow => (),
435445
_ => unreachable!("error"),
436446
}
437447

438-
let mut req = TestResponse::default()
448+
let req = TestResponse::default()
439449
.set_payload(Bytes::from_static(b"test"))
440450
.finish();
441451
assert_eq!(req.body().await.ok().unwrap(), Bytes::from_static(b"test"));
442452

443-
let mut req = TestResponse::default()
453+
let req = TestResponse::default()
444454
.set_payload(Bytes::from_static(b"11111111111111"))
445455
.finish();
446456
match req.body().limit(5).await.err().unwrap() {
@@ -466,20 +476,20 @@ mod tests {
466476

467477
#[crate::rt_test]
468478
async fn test_json_body() {
469-
let mut req = TestResponse::default().finish();
470-
let json = JsonBody::<MyObject>::new(&mut req).await;
479+
let req = TestResponse::default().finish();
480+
let json = JsonBody::<MyObject>::new(&req).await;
471481
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
472482

473-
let mut req = TestResponse::default()
483+
let req = TestResponse::default()
474484
.header(
475485
header::CONTENT_TYPE,
476486
header::HeaderValue::from_static("application/text"),
477487
)
478488
.finish();
479-
let json = JsonBody::<MyObject>::new(&mut req).await;
489+
let json = JsonBody::<MyObject>::new(&req).await;
480490
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
481491

482-
let mut req = TestResponse::default()
492+
let req = TestResponse::default()
483493
.header(
484494
header::CONTENT_TYPE,
485495
header::HeaderValue::from_static("application/json"),
@@ -490,13 +500,13 @@ mod tests {
490500
)
491501
.finish();
492502

493-
let json = JsonBody::<MyObject>::new(&mut req).limit(100).await;
503+
let json = JsonBody::<MyObject>::new(&req).limit(100).await;
494504
assert!(json_eq(
495505
json.err().unwrap(),
496506
JsonPayloadError::Payload(PayloadError::Overflow)
497507
));
498508

499-
let mut req = TestResponse::default()
509+
let req = TestResponse::default()
500510
.header(
501511
header::CONTENT_TYPE,
502512
header::HeaderValue::from_static("application/json"),
@@ -508,7 +518,7 @@ mod tests {
508518
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
509519
.finish();
510520

511-
let json = JsonBody::<MyObject>::new(&mut req).await;
521+
let json = JsonBody::<MyObject>::new(&req).await;
512522
assert_eq!(
513523
json.ok().unwrap(),
514524
MyObject {

0 commit comments

Comments
 (0)