Skip to main content

BudgetedStringWriter

Struct BudgetedStringWriter 

Source
pub struct BudgetedStringWriter<'a, R, Q = u64>{ /* private fields */ }
Expand description

Collects a UTF-8 string while checking a finite byte budget incrementally.

The writer is constructed and committed by ResourceBudget::try_write_string. A failed render drops the buffered prefix and leaves the budget unchanged.

Use Self::as_fmt for formatting APIs or Self::as_io for byte-oriented I/O. The returned adapter borrows this writer, so the render callback must finish using it before returning.

§Type Parameters

  • R - Caller-defined resource identity retained by limits and errors.
  • Q - Exact unsigned quantity used for measurements and accounting.

§Examples

use std::fmt::Write as _;
use qubit_budget::ResourceBudget;

let mut budget = ResourceBudget::new("response bytes", 16_u64);
let output = budget
    .try_write_string(|writer| {
        let mut formatted = writer.as_fmt();
        write!(&mut formatted, "status={}", 200)
    })
    .expect("the rendered response should fit");

assert_eq!(output, "status=200");
assert_eq!(budget.used(), 10);

Implementations§

Source§

impl<'a, R, Q> BudgetedStringWriter<'a, R, Q>
where R: Clone, Q: ResourceQuantity,

Source

pub fn as_fmt(&mut self) -> impl Write + '_

Returns a formatting writer view over the current transaction.

§Returns

Returns a formatting writer view over the current transaction.

Source

pub fn as_io(&mut self) -> impl Write + '_

Returns an I/O writer view over the current transaction.

§Returns

Returns an I/O writer view over the current transaction.

Auto Trait Implementations§

§

impl<'a, R, Q> Freeze for BudgetedStringWriter<'a, R, Q>
where &'a ResourceBudget<R, Q>: Freeze, Option<WriterFailure<R, Q>>: Freeze,

§

impl<'a, R, Q> RefUnwindSafe for BudgetedStringWriter<'a, R, Q>
where &'a ResourceBudget<R, Q>: RefUnwindSafe, Option<WriterFailure<R, Q>>: RefUnwindSafe,

§

impl<'a, R, Q> Send for BudgetedStringWriter<'a, R, Q>
where &'a ResourceBudget<R, Q>: Send, Option<WriterFailure<R, Q>>: Send,

§

impl<'a, R, Q> Sync for BudgetedStringWriter<'a, R, Q>
where &'a ResourceBudget<R, Q>: Sync, Option<WriterFailure<R, Q>>: Sync,

§

impl<'a, R, Q> Unpin for BudgetedStringWriter<'a, R, Q>
where &'a ResourceBudget<R, Q>: Unpin, Option<WriterFailure<R, Q>>: Unpin,

§

impl<'a, R, Q> UnsafeUnpin for BudgetedStringWriter<'a, R, Q>
where &'a ResourceBudget<R, Q>: UnsafeUnpin, Option<WriterFailure<R, Q>>: UnsafeUnpin,

§

impl<'a, R, Q> UnwindSafe for BudgetedStringWriter<'a, R, Q>
where &'a ResourceBudget<R, Q>: UnwindSafe, Option<WriterFailure<R, Q>>: 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, 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, 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.