Struct smoldot_light::platform::read_write::ReadWrite
source · pub struct ReadWrite<TNow> {
pub now: TNow,
pub incoming_buffer: Vec<u8>,
pub expected_incoming_bytes: Option<usize>,
pub read_bytes: usize,
pub write_buffers: Vec<Vec<u8>>,
pub write_bytes_queued: usize,
pub write_bytes_queueable: Option<usize>,
pub wake_up_after: Option<TNow>,
}
Fields§
§now: TNow
§incoming_buffer: Vec<u8>
Buffer of socket data ready to be processed.
expected_incoming_bytes: Option<usize>
Number of bytes that ReadWrite::incoming_buffer
should contain. None
if the remote
has closed their reading side of the socket.
read_bytes: usize
Total number of bytes that have been read from ReadWrite::incoming_buffer
.
ReadWrite::incoming_buffer
must have been advanced after these bytes.
write_buffers: Vec<Vec<u8>>
List of buffers containing data to the written out. The consumer of the ReadWrite
is
expected to add buffers.
write_bytes_queued: usize
Amount of data already queued, both outside and including ReadWrite::write_buffers
.
write_bytes_queueable: Option<usize>
Number of additional bytes that are allowed to be pushed to ReadWrite::write_buffers
.
None
if the writing side of the stream is closed.
wake_up_after: Option<TNow>
If Some
, the socket must be waken up after the given TNow
is reached.
Implementations§
source§impl<TNow> ReadWrite<TNow>
impl<TNow> ReadWrite<TNow>
sourcepub fn is_dead(&self) -> bool
pub fn is_dead(&self) -> bool
Returns true if the connection should be considered dead. That is, both
ReadWrite::expected_incoming_bytes
is None
and ReadWrite::write_bytes_queueable
is None
.
sourcepub fn close_write(&mut self)
pub fn close_write(&mut self)
Sets the writing side of the connection to closed.
This is simply a shortcut for setting ReadWrite::write_bytes_queueable
to None
.
sourcepub fn incoming_buffer_available(&self) -> usize
pub fn incoming_buffer_available(&self) -> usize
Returns the size of the data available in the incoming buffer.
sourcepub fn discard_all_incoming(&mut self)
pub fn discard_all_incoming(&mut self)
Discards all the incoming data. Updates ReadWrite::read_bytes
and decreases
ReadWrite::expected_incoming_bytes
by the number of consumed bytes.
sourcepub fn incoming_bytes_take(
&mut self,
num: usize,
) -> Result<Option<Vec<u8>>, IncomingBytesTakeError>
pub fn incoming_bytes_take( &mut self, num: usize, ) -> Result<Option<Vec<u8>>, IncomingBytesTakeError>
Extract a certain number of bytes from the read buffer.
On success, updates ReadWrite::read_bytes
and decreases
ReadWrite::expected_incoming_bytes
by the number of consumed bytes.
If not enough bytes are available, returns None
and sets
ReadWrite::expected_incoming_bytes
to the requested number of bytes.
sourcepub fn incoming_bytes_take_array<const N: usize>(
&mut self,
) -> Result<Option<[u8; N]>, IncomingBytesTakeError>
pub fn incoming_bytes_take_array<const N: usize>( &mut self, ) -> Result<Option<[u8; N]>, IncomingBytesTakeError>
Same as ReadWrite::incoming_bytes_take_array
, but reads a number of bytes as a
compile-time constant.
sourcepub fn incoming_bytes_take_leb128(
&mut self,
max_decoded_number: usize,
) -> Result<Option<usize>, IncomingBytesTakeLeb128Error>
pub fn incoming_bytes_take_leb128( &mut self, max_decoded_number: usize, ) -> Result<Option<usize>, IncomingBytesTakeLeb128Error>
Extract an LEB128-encoded number from the start of the read buffer.
On success, updates ReadWrite::read_bytes
and decreases
ReadWrite::expected_incoming_bytes
by the number of consumed bytes.
If not enough bytes are available, returns None
and sets
ReadWrite::expected_incoming_bytes
to the required number of bytes.
Must be passed the maximum value that this function can return on success. An error is returned if the value sent by the remote is higher than this maximum. This parameter, while not strictly necessary, is here for safety, as it is easy to forget to check the value against a maximum.
sourcepub fn write_from_vec(&mut self, data: &mut Vec<u8>)
pub fn write_from_vec(&mut self, data: &mut Vec<u8>)
Copies as much as possible from the content of data
to ReadWrite::write_buffers
and updates ReadWrite::write_bytes_queued
and ReadWrite::write_bytes_queueable
.
The bytes that have been written are removed from data
.
This function is recommended only if the Vec
is small.
sourcepub fn write_from_vec_deque(&mut self, data: &mut VecDeque<u8>)
pub fn write_from_vec_deque(&mut self, data: &mut VecDeque<u8>)
Copies as much as possible from the content of data
to ReadWrite::write_buffers
and updates ReadWrite::write_bytes_queued
and ReadWrite::write_bytes_queueable
.
The bytes that have been written are removed from data
.
sourcepub fn write_out(&mut self, data: Vec<u8>)
pub fn write_out(&mut self, data: Vec<u8>)
Adds the data
to ReadWrite::write_buffers
, increases
ReadWrite::write_bytes_queued
, and decreases ReadWrite::write_bytes_queueable
.
§Panic
Panics if data.len() > write_bytes_queueable
.
Panics if the writing side is closed and data
isn’t empty.
sourcepub fn wake_up_after(&mut self, after: &TNow)
pub fn wake_up_after(&mut self, after: &TNow)
Sets ReadWrite::wake_up_after
to min(wake_up_after, after)
.
sourcepub fn wake_up_asap(&mut self)where
TNow: Clone,
pub fn wake_up_asap(&mut self)where
TNow: Clone,
Sets ReadWrite::wake_up_after
to the value in ReadWrite::now
.
Auto Trait Implementations§
impl<TNow> Freeze for ReadWrite<TNow>where
TNow: Freeze,
impl<TNow> RefUnwindSafe for ReadWrite<TNow>where
TNow: RefUnwindSafe,
impl<TNow> Send for ReadWrite<TNow>where
TNow: Send,
impl<TNow> Sync for ReadWrite<TNow>where
TNow: Sync,
impl<TNow> Unpin for ReadWrite<TNow>where
TNow: Unpin,
impl<TNow> UnwindSafe for ReadWrite<TNow>where
TNow: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more