Skip to main content

LimitWriter

Struct LimitWriter 

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

Writer wrapper that accepts at most a fixed number of bytes.

Once the remaining limit reaches zero, writes return Ok(0) without touching the inner writer. Callers using Write::write_all will therefore receive the standard write-zero error when trying to write beyond the limit.

§Examples

use std::io::Write;

use qubit_io::LimitWriter;

let mut writer = LimitWriter::new(Vec::new(), 3);

assert_eq!(3, writer.write(b"abcdef")?);
assert_eq!(0, writer.remaining());
assert_eq!(0, writer.write(b"x")?);
assert_eq!(b"abc", writer.get_ref().as_slice());

Implementations§

Source§

impl<W> LimitWriter<W>

Source

pub fn new(inner: W, limit: u64) -> Self

Creates a writer that accepts at most limit bytes.

§Parameters
  • inner: Writer to wrap.
  • limit: Maximum number of bytes that may be written through this wrapper.
§Returns

A new limited writer.

Source

pub fn remaining(&self) -> u64

Returns the number of bytes still accepted by this wrapper.

§Returns

Remaining writable byte count before the wrapper reports zero writes.

Source

pub fn get_ref(&self) -> &W

Returns an immutable reference to the wrapped writer.

§Returns

The wrapped writer reference.

Source

pub fn get_mut(&mut self) -> &mut W

Returns a mutable reference to the wrapped writer.

§Returns

The wrapped writer reference.

Source

pub fn into_inner(self) -> W

Consumes this wrapper and returns the wrapped writer.

§Returns

The wrapped writer.

Trait Implementations§

Source§

impl<W> Write for LimitWriter<W>
where W: Write,

Source§

fn write(&mut self, buffer: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

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

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<W> UnwindSafe for LimitWriter<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> BinaryWriteExt for T
where T: Write + ?Sized,

Source§

fn write_u8(&mut self, value: u8) -> Result<(), Error>

Writes one unsigned byte. Read more
Source§

fn write_i8(&mut self, value: i8) -> Result<(), Error>

Writes one signed byte. Read more
Source§

fn write_u16_be(&mut self, value: u16) -> Result<(), Error>

Writes a big-endian u16. Read more
Source§

fn write_u16_le(&mut self, value: u16) -> Result<(), Error>

Writes a little-endian u16. Read more
Source§

fn write_i16_be(&mut self, value: i16) -> Result<(), Error>

Writes a big-endian i16. Read more
Source§

fn write_i16_le(&mut self, value: i16) -> Result<(), Error>

Writes a little-endian i16. Read more
Source§

fn write_u32_be(&mut self, value: u32) -> Result<(), Error>

Writes a big-endian u32. Read more
Source§

fn write_u32_le(&mut self, value: u32) -> Result<(), Error>

Writes a little-endian u32. Read more
Source§

fn write_i32_be(&mut self, value: i32) -> Result<(), Error>

Writes a big-endian i32. Read more
Source§

fn write_i32_le(&mut self, value: i32) -> Result<(), Error>

Writes a little-endian i32. Read more
Source§

fn write_u64_be(&mut self, value: u64) -> Result<(), Error>

Writes a big-endian u64. Read more
Source§

fn write_u64_le(&mut self, value: u64) -> Result<(), Error>

Writes a little-endian u64. Read more
Source§

fn write_i64_be(&mut self, value: i64) -> Result<(), Error>

Writes a big-endian i64. Read more
Source§

fn write_i64_le(&mut self, value: i64) -> Result<(), Error>

Writes a little-endian i64. Read more
Source§

fn write_u128_be(&mut self, value: u128) -> Result<(), Error>

Writes a big-endian u128. Read more
Source§

fn write_u128_le(&mut self, value: u128) -> Result<(), Error>

Writes a little-endian u128. Read more
Source§

fn write_i128_be(&mut self, value: i128) -> Result<(), Error>

Writes a big-endian i128. Read more
Source§

fn write_i128_le(&mut self, value: i128) -> Result<(), Error>

Writes a little-endian i128. Read more
Source§

fn write_f32_be(&mut self, value: f32) -> Result<(), Error>

Writes a big-endian IEEE-754 f32. Read more
Source§

fn write_f32_le(&mut self, value: f32) -> Result<(), Error>

Writes a little-endian IEEE-754 f32. Read more
Source§

fn write_f64_be(&mut self, value: f64) -> Result<(), Error>

Writes a big-endian IEEE-754 f64. Read more
Source§

fn write_f64_le(&mut self, value: f64) -> Result<(), Error>

Writes a little-endian IEEE-754 f64. Read more
Source§

fn write_u16(&mut self, value: u16, order: ByteOrder) -> Result<()>

Writes a u16 using order. Read more
Source§

fn write_i16(&mut self, value: i16, order: ByteOrder) -> Result<()>

Writes an i16 using order. Read more
Source§

fn write_u32(&mut self, value: u32, order: ByteOrder) -> Result<()>

Writes a u32 using order. Read more
Source§

fn write_i32(&mut self, value: i32, order: ByteOrder) -> Result<()>

Writes an i32 using order. Read more
Source§

fn write_u64(&mut self, value: u64, order: ByteOrder) -> Result<()>

Writes a u64 using order. Read more
Source§

fn write_i64(&mut self, value: i64, order: ByteOrder) -> Result<()>

Writes an i64 using order. Read more
Source§

fn write_u128(&mut self, value: u128, order: ByteOrder) -> Result<()>

Writes a u128 using order. Read more
Source§

fn write_i128(&mut self, value: i128, order: ByteOrder) -> Result<()>

Writes an i128 using order. Read more
Source§

fn write_f32(&mut self, value: f32, order: ByteOrder) -> Result<()>

Writes an IEEE-754 f32 using order. Read more
Source§

fn write_f64(&mut self, value: f64, order: ByteOrder) -> Result<()>

Writes an IEEE-754 f64 using order. 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, 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> Leb128WriteExt for T
where T: Write + ?Sized,

Source§

fn write_uleb_u8(&mut self, value: u8) -> Result<(), Error>

Writes an unsigned LEB128 u8. Read more
Source§

fn write_uleb_u16(&mut self, value: u16) -> Result<(), Error>

Writes an unsigned LEB128 u16. Read more
Source§

fn write_uleb_u32(&mut self, value: u32) -> Result<(), Error>

Writes an unsigned LEB128 u32. Read more
Source§

fn write_uleb_u64(&mut self, value: u64) -> Result<(), Error>

Writes an unsigned LEB128 u64. Read more
Source§

fn write_uleb_u128(&mut self, value: u128) -> Result<(), Error>

Writes an unsigned LEB128 u128. Read more
Source§

fn write_uleb_usize(&mut self, value: usize) -> Result<(), Error>

Writes an unsigned LEB128 usize. Read more
Source§

fn write_sleb_i8(&mut self, value: i8) -> Result<(), Error>

Writes a signed LEB128 i8. Read more
Source§

fn write_sleb_i16(&mut self, value: i16) -> Result<(), Error>

Writes a signed LEB128 i16. Read more
Source§

fn write_sleb_i32(&mut self, value: i32) -> Result<(), Error>

Writes a signed LEB128 i32. Read more
Source§

fn write_sleb_i64(&mut self, value: i64) -> Result<(), Error>

Writes a signed LEB128 i64. Read more
Source§

fn write_sleb_i128(&mut self, value: i128) -> Result<(), Error>

Writes a signed LEB128 i128. Read more
Source§

fn write_sleb_isize(&mut self, value: isize) -> Result<(), Error>

Writes a signed LEB128 isize. Read more
Source§

impl<T> StringWriteExt for T
where T: Write + ?Sized,

Source§

fn write_utf8_string_uleb(&mut self, value: &str) -> Result<(), Error>

Writes a UTF-8 string with an unsigned LEB128 byte-length prefix. Read more
Source§

fn write_utf8_string_u16_be(&mut self, value: &str) -> Result<(), Error>

Writes a UTF-8 string with a big-endian u16 byte-length prefix. Read more
Source§

fn write_utf8_string_u16_le(&mut self, value: &str) -> Result<(), Error>

Writes a UTF-8 string with a little-endian u16 byte-length prefix. Read more
Source§

fn write_utf8_string_u32_be(&mut self, value: &str) -> Result<(), Error>

Writes a UTF-8 string with a big-endian u32 byte-length prefix. Read more
Source§

fn write_utf8_string_u32_le(&mut self, value: &str) -> Result<(), Error>

Writes a UTF-8 string with a little-endian u32 byte-length prefix. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> ZigZagWriteExt for T
where T: Write + ?Sized,

Source§

fn write_zigzag_i8(&mut self, value: i8) -> Result<(), Error>

Writes a ZigZag encoded i8. Read more
Source§

fn write_zigzag_i16(&mut self, value: i16) -> Result<(), Error>

Writes a ZigZag encoded i16. Read more
Source§

fn write_zigzag_i32(&mut self, value: i32) -> Result<(), Error>

Writes a ZigZag encoded i32. Read more
Source§

fn write_zigzag_i64(&mut self, value: i64) -> Result<(), Error>

Writes a ZigZag encoded i64. Read more
Source§

fn write_zigzag_i128(&mut self, value: i128) -> Result<(), Error>

Writes a ZigZag encoded i128. Read more
Source§

fn write_zigzag_isize(&mut self, value: isize) -> Result<(), Error>

Writes a ZigZag encoded isize. Read more