mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-04 13:27:39 +03:00
Custom panic message for nested buffer borrow
This commit is contained in:
parent
af018b46fe
commit
f61f4cc8c5
3 changed files with 19 additions and 9 deletions
|
@ -1,5 +1,9 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.5] - 2023-01-27
|
||||||
|
|
||||||
|
* Custom panic message for nested buffer borrow
|
||||||
|
|
||||||
## [0.2.4] - 2023-01-26
|
## [0.2.4] - 2023-01-26
|
||||||
|
|
||||||
* Refactor write task management
|
* Refactor write task management
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ntex-io"
|
name = "ntex-io"
|
||||||
version = "0.2.4"
|
version = "0.2.5"
|
||||||
authors = ["ntex contributors <team@ntex.rs>"]
|
authors = ["ntex contributors <team@ntex.rs>"]
|
||||||
description = "Utilities for encoding and decoding frames"
|
description = "Utilities for encoding and decoding frames"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use std::{any, fmt, hash, io, time};
|
use std::{any, cell, fmt, hash, io, time};
|
||||||
|
|
||||||
use ntex_bytes::{BytesVec, PoolRef};
|
use ntex_bytes::{BytesVec, PoolRef};
|
||||||
use ntex_codec::{Decoder, Encoder};
|
use ntex_codec::{Decoder, Encoder};
|
||||||
|
|
||||||
use super::{io::Flags, timer, types, Filter, IoRef, OnDisconnect, WriteBuf};
|
use super::{buf::Stack, io::Flags, timer, types, Filter, IoRef, OnDisconnect, WriteBuf};
|
||||||
|
|
||||||
impl IoRef {
|
impl IoRef {
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -132,9 +132,7 @@ impl IoRef {
|
||||||
where
|
where
|
||||||
U: Decoder,
|
U: Decoder,
|
||||||
{
|
{
|
||||||
self.0
|
borrow_buffer(&self.0.buffer)
|
||||||
.buffer
|
|
||||||
.borrow_mut()
|
|
||||||
.first_read_buf()
|
.first_read_buf()
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.map(|b| codec.decode_vec(b))
|
.map(|b| codec.decode_vec(b))
|
||||||
|
@ -159,7 +157,7 @@ impl IoRef {
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut WriteBuf<'_>) -> R,
|
F: FnOnce(&mut WriteBuf<'_>) -> R,
|
||||||
{
|
{
|
||||||
let mut buffer = self.0.buffer.borrow_mut();
|
let mut buffer = borrow_buffer(&self.0.buffer);
|
||||||
let result = buffer.write_buf(self, 0, f);
|
let result = buffer.write_buf(self, 0, f);
|
||||||
self.0
|
self.0
|
||||||
.filter
|
.filter
|
||||||
|
@ -174,7 +172,7 @@ impl IoRef {
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut BytesVec) -> R,
|
F: FnOnce(&mut BytesVec) -> R,
|
||||||
{
|
{
|
||||||
let mut buffer = self.0.buffer.borrow_mut();
|
let mut buffer = borrow_buffer(&self.0.buffer);
|
||||||
let result = f(buffer.first_write_buf(self));
|
let result = f(buffer.first_write_buf(self));
|
||||||
self.0
|
self.0
|
||||||
.filter
|
.filter
|
||||||
|
@ -191,7 +189,7 @@ impl IoRef {
|
||||||
F: FnOnce(&mut BytesVec) -> R,
|
F: FnOnce(&mut BytesVec) -> R,
|
||||||
{
|
{
|
||||||
// use top most buffer
|
// use top most buffer
|
||||||
let mut buffer = self.0.buffer.borrow_mut();
|
let mut buffer = borrow_buffer(&self.0.buffer);
|
||||||
let buf = buffer.first_read_buf();
|
let buf = buffer.first_read_buf();
|
||||||
if buf.is_none() {
|
if buf.is_none() {
|
||||||
*buf = Some(self.memory_pool().get_read_buf());
|
*buf = Some(self.memory_pool().get_read_buf());
|
||||||
|
@ -253,6 +251,14 @@ impl fmt::Debug for IoRef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn borrow_buffer(buf: &cell::RefCell<Stack>) -> cell::RefMut<'_, Stack> {
|
||||||
|
if let Ok(r) = buf.try_borrow_mut() {
|
||||||
|
r
|
||||||
|
} else {
|
||||||
|
panic!("Nested access to read/write buffers are not allowed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue