Skip to main content

FrameReader

Struct FrameReader 

Source
pub struct FrameReader<R> { /* private fields */ }
Expand description

A reader of frames, object chunks, and pull bodies on a stream.

A frame takes three reads and a chunk takes two, so a caller wraps a raw socket or pipe in a buffered reader. A buffered reader that passes a large read through to the stream does not copy the object bytes a second time.

The futures of the reader are not cancel-safe. A call that is dropped before it completes can leave part of a frame or of a chunk length read. The next call then reads from the middle of it. After a canceled call, the caller must drop the reader.

Implementations§

Source§

impl<R: AsyncRead + Unpin> FrameReader<R>

Source

pub fn new(inner: R) -> Self

Creates a reader with the limit MIN_FRAME_LIMIT.

Source

pub fn set_limit(&mut self, limit: u32)

Sets the frame and chunk limit of the reader.

The value is clamped to MIN_FRAME_LIMIT..=MAX_FRAME_LIMIT. A client sets a read limit of its own here, for example MAX_FRAME, and gives the max-frame of the server to its FrameWriter alone. The frame rules give the reason.

Source

pub fn limit(&self) -> u32

Returns the frame and chunk limit.

Source

pub async fn read_message(&mut self) -> Result<Option<Message>>

Reads the next message.

Ok(None) is an end of file at a frame boundary. After an ObjectHeader, and after a GetReply whose found is true, the caller must read the body to its end before the next message. It reads the body with read_object_data or object_body.

§Errors
  • Error::Protocol if the body of the previous message is not read to its end.
  • Error::Protocol if the frame length is 0, if the kind byte is not a known kind, or if the body does not decode.
  • Error::LimitExceeded if the frame length is more than the limit. The reader checks the length before it reads or allocates the body.
  • Error::Io of kind UnexpectedEof if the stream ends inside a frame.
  • Error::Io if a read of the stream fails.
Source

pub fn object_body(&mut self) -> ObjectBody<'_, R>

Returns an ObjectBody that reads the current object or pull body.

Source

pub async fn read_object_data(&mut self, buf: &mut [u8]) -> Result<ObjectRead>

Reads bytes of the current object or pull body into buf.

A chunk longer than buf arrives over several calls, so buf bounds the memory. Inside a body, an empty buf reads nothing and gives ObjectRead::Data(0).

If the call meets the abandon marker, it reads the message that must follow the marker. In an object of the push, an Abort message gives ObjectRead::Abandoned.

§Errors
  • Error::Protocol if the reader is not inside a body.
  • Error::LimitExceeded if a chunk length is more than the limit.
  • In a body of the pull, the Error variant of the code in the Error message after the marker, for example Error::Internal.
  • Error::Protocol if a message of another kind follows the marker.
  • Error::Io of kind UnexpectedEof if the stream ends inside a chunk, or after the marker and before the next message.
  • Error::Io if a read of the stream fails.
  • The errors of read_message for the message after the marker.
Source

pub fn get_ref(&self) -> &R

Returns a reference to the underlying stream.

If the stream is a buffered reader, a caller can look at the bytes in the buffer with no read. An example is a check for a whole frame in the buffer.

Source

pub fn into_inner(self) -> R

Returns the underlying stream.

Trait Implementations§

Source§

impl<R: Debug> Debug for FrameReader<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Forget for FrameReader<R>

§

impl<R> Freeze for FrameReader<R>
where R: Freeze,

§

impl<R> Move for FrameReader<R>

§

impl<R> RefUnwindSafe for FrameReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for FrameReader<R>
where R: Send,

§

impl<R> Sync for FrameReader<R>
where R: Sync,

§

impl<R> Unpin for FrameReader<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for FrameReader<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for FrameReader<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more