Skip to main content

FrameWriter

Struct FrameWriter 

Source
pub struct FrameWriter<W> { /* private fields */ }
Expand description

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

The writer enters the body state when it writes an ObjectHeader, or a GetReply whose found is true.

The writer holds no object bytes and does not flush. The caller flushes with flush, or wraps the stream in a buffered writer. A frame is one write, and a chunk is two: the length and the data. A caller wraps a raw socket or pipe in a buffered writer.

The futures of the writer are not cancel-safe. A call that is dropped before it completes can leave part of a frame or a chunk written. After a canceled call, the caller must drop the writer.

Implementations§

Source§

impl<W: AsyncWrite + Unpin> FrameWriter<W>

Source

pub fn new(inner: W) -> Self

Creates a writer with the limit MIN_FRAME_LIMIT.

Examples found in repository?
examples/send_pass.rs (line 57)
37async fn scripted_replies() -> io::Result<Vec<u8>> {
38    let msgs = [
39        Message::HelloReply(HelloReply {
40            version: 1,
41            mode: "archive".into(),
42            collection_id: None,
43            max_frame: MIN_FRAME_LIMIT,
44            max_have: 16_384,
45            encodings: vec![Encoding::Raw, Encoding::Deflate],
46            parallel_uploads: 1,
47            refs: vec![RefState {
48                name: "main".into(),
49                commit: None,
50            }],
51        }),
52        Message::ObjectsReply(ObjectsReply {
53            objects: 0,
54            payload_bytes: 0,
55        }),
56    ];
57    let mut w = FrameWriter::new(Vec::new());
58    for msg in &msgs {
59        w.write_message(msg).await.map_err(io::Error::other)?;
60    }
61    Ok(w.into_inner())
62}
Source

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

Sets the frame and chunk limit to the max-frame that the peer announced.

The value is clamped to MIN_FRAME_LIMIT..=MAX_FRAME_LIMIT. HelloReply::max_frame holds the announced value. The writer applies the limit to each frame and each chunk that it writes after the call.

Source

pub async fn write_message(&mut self, msg: &Message) -> Result<()>

Writes one message as a frame.

After an ObjectHeader, and after a GetReply whose found is true, the writer is in the body state.

§Errors

If the call returns Error::Protocol or Error::LimitExceeded, it writes nothing.

Examples found in repository?
examples/send_pass.rs (line 59)
37async fn scripted_replies() -> io::Result<Vec<u8>> {
38    let msgs = [
39        Message::HelloReply(HelloReply {
40            version: 1,
41            mode: "archive".into(),
42            collection_id: None,
43            max_frame: MIN_FRAME_LIMIT,
44            max_have: 16_384,
45            encodings: vec![Encoding::Raw, Encoding::Deflate],
46            parallel_uploads: 1,
47            refs: vec![RefState {
48                name: "main".into(),
49                commit: None,
50            }],
51        }),
52        Message::ObjectsReply(ObjectsReply {
53            objects: 0,
54            payload_bytes: 0,
55        }),
56    ];
57    let mut w = FrameWriter::new(Vec::new());
58    for msg in &msgs {
59        w.write_message(msg).await.map_err(io::Error::other)?;
60    }
61    Ok(w.into_inner())
62}
Source

pub async fn write_object_data(&mut self, data: &[u8]) -> Result<()>

Writes bytes of the current object or pull body as chunks.

Each chunk holds at most the limit. Inside a body, an empty data writes nothing. Each call with data writes one chunk or more, and each chunk costs 4 bytes of framing and one read call on the peer. A caller gives the object bytes in pieces of 64 KiB or more.

§Errors
Source

pub async fn end_object(&mut self) -> Result<()>

Ends the current object or pull body with a chunk of length 0.

§Errors
Source

pub async fn abandon_object(&mut self) -> Result<()>

Abandons the current object of the push.

The call writes the marker ABANDON and then an Abort message.

§Errors
  • Error::Protocol if the writer is not inside an object of the push, for example if it is inside a body of the pull.
  • Error::Io if a write to the stream fails.
Source

pub async fn abandon_body(&mut self, error: &ErrorMessage) -> Result<()>

Abandons the current body of the pull.

The call writes the marker ABANDON and then error as an Error message. The caller gives the code, for example internal for a failed read of the file.

§Errors
  • Error::Protocol if the writer is not inside a body of the pull, for example if it is inside an object of the push.
  • Error::Protocol if error does not encode. The call writes nothing.
  • Error::LimitExceeded if the frame of error is over the limit. The call writes nothing.
  • Error::Io if a write to the stream fails.
Source

pub async fn flush(&mut self) -> Result<()>

Flushes the underlying stream.

§Errors

Error::Io if the flush of the stream fails.

Source

pub fn into_inner(self) -> W

Returns the underlying stream.

Examples found in repository?
examples/send_pass.rs (line 61)
37async fn scripted_replies() -> io::Result<Vec<u8>> {
38    let msgs = [
39        Message::HelloReply(HelloReply {
40            version: 1,
41            mode: "archive".into(),
42            collection_id: None,
43            max_frame: MIN_FRAME_LIMIT,
44            max_have: 16_384,
45            encodings: vec![Encoding::Raw, Encoding::Deflate],
46            parallel_uploads: 1,
47            refs: vec![RefState {
48                name: "main".into(),
49                commit: None,
50            }],
51        }),
52        Message::ObjectsReply(ObjectsReply {
53            objects: 0,
54            payload_bytes: 0,
55        }),
56    ];
57    let mut w = FrameWriter::new(Vec::new());
58    for msg in &msgs {
59        w.write_message(msg).await.map_err(io::Error::other)?;
60    }
61    Ok(w.into_inner())
62}

Trait Implementations§

Source§

impl<W: Debug> Debug for FrameWriter<W>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<W> Forget for FrameWriter<W>

§

impl<W> Freeze for FrameWriter<W>
where W: Freeze,

§

impl<W> Move for FrameWriter<W>

§

impl<W> RefUnwindSafe for FrameWriter<W>
where W: RefUnwindSafe,

§

impl<W> Send for FrameWriter<W>
where W: Send,

§

impl<W> Sync for FrameWriter<W>
where W: Sync,

§

impl<W> Unpin for FrameWriter<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for FrameWriter<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for FrameWriter<W>
where W: 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