Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic_on_option_buffer #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions monoio-rustls/src/safe_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ impl Default for SafeRead {

impl SafeRead {
pub(crate) async fn do_io<IO: AsyncReadRent>(&mut self, mut io: IO) -> io::Result<usize> {
if self.buffer.is_none() {
// if called do_io after the time async read/write had been cancelled,
// buffer will be none, so we return Other error.
return Err(io::ErrorKind::Other.into());
}
// if there are some data inside the buffer, just return.
let buffer = self.buffer.as_ref().expect("buffer ref expected");
if !buffer.is_empty() {
Expand Down Expand Up @@ -134,6 +139,11 @@ impl SafeRead {

impl io::Read for SafeRead {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.buffer.is_none() {
// if called read after the time async read/write had been cancelled,
// buffer will be none, so we return Other error.
return Err(io::ErrorKind::Other.into());
}
// if buffer is empty, return WoundBlock.
let buffer = self.buffer.as_mut().expect("buffer mut expected");
if buffer.is_empty() {
Expand Down Expand Up @@ -187,6 +197,11 @@ impl Default for SafeWrite {

impl SafeWrite {
pub(crate) async fn do_io<IO: AsyncWriteRent>(&mut self, mut io: IO) -> io::Result<usize> {
if self.buffer.is_none() {
// if called do_io after the time async read/write had been cancelled,
// buffer will be none, so we return Other error.
return Err(io::ErrorKind::Other.into());
}
// if the buffer is empty, just return.
let buffer = self.buffer.as_ref().expect("buffer ref expected");
if buffer.is_empty() {
Expand All @@ -213,6 +228,11 @@ impl SafeWrite {

impl io::Write for SafeWrite {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.buffer.is_none() {
// if called write after the time async read/write had been cancelled,
// buffer will be none, so we return Other error.
return Err(io::ErrorKind::Other.into());
}
// if there is too much data inside the buffer, return WoundBlock
let buffer = self.buffer.as_mut().expect("buffer mut expected");
if !matches!(self.status, WriteStatus::Ok) {
Expand All @@ -233,6 +253,11 @@ impl io::Write for SafeWrite {
}

fn flush(&mut self) -> io::Result<()> {
if self.buffer.is_none() {
// if called flush after the time async read/write had been cancelled,
// buffer will be none, so we return Other error.
return Err(io::ErrorKind::Other.into());
}
let buffer = self.buffer.as_mut().expect("buffer mut expected");
if !matches!(self.status, WriteStatus::Ok) {
match std::mem::replace(&mut self.status, WriteStatus::Ok) {
Expand Down