Skip to main content

Output

Struct Output 

Source
pub struct Output<'a> { /* private fields */ }
Expand description

Where an engine writes: the reply body, reused between requests, with a size budget.

use web_net::Output;

let mut buf = Vec::new();
let mut out = Output::new(&mut buf, 64);
out.write_str("<p>hi</p>")?;
assert_eq!(out.remaining(), 55);
assert_eq!(buf, b"<p>hi</p>");

Implementations§

Source§

impl<'a> Output<'a>

Source

pub fn new(buf: &'a mut Vec<u8>, limit: usize) -> Self

An output over buf, refusing to grow past limit bytes.

use web_net::Output;

let mut buf = Vec::new();
let mut out = Output::new(&mut buf, 4);
assert!(out.write(b"1234").is_ok());
assert_eq!(out.write(b"5").unwrap_err().code(), "render.too_large");
Source

pub fn write(&mut self, bytes: &[u8]) -> Result<(), RenderError>

Appends bytes.

use web_net::Output;

let mut buf = Vec::new();
let mut out = Output::new(&mut buf, 64);
out.write(b"<p>")?;
assert_eq!(out.len(), 3);
§Errors

render.too_large when the page would pass the budget.

Source

pub fn write_str(&mut self, s: &str) -> Result<(), RenderError>

Appends a string.

use web_net::{Output, escape_html};

let mut buf = Vec::new();
let mut out = Output::new(&mut buf, 64);
out.write_str(&escape_html("a<b"))?;
assert_eq!(buf, b"a&lt;b");
§Errors

As write.

Source

pub fn buffer(&mut self) -> &mut Vec<u8> ⓘ

The buffer itself, for an engine that writes into a Vec<u8>. The budget is checked when the engine returns: a page past it is refused with render.too_large.

use std::io::Write;
use web_net::Output;

let mut buf = Vec::new();
let mut out = Output::new(&mut buf, 64);
write!(out.buffer(), "{} items", 3).unwrap();
assert_eq!(out.len(), 7);
Source

pub fn len(&self) -> usize

Bytes written so far.

use web_net::Output;

let mut buf = Vec::new();
let mut out = Output::new(&mut buf, 64);
out.write_str("hello")?;
assert_eq!(out.len(), 5);
Source

pub fn is_empty(&self) -> bool

Whether nothing was written yet.

use web_net::Output;

let mut buf = Vec::new();
let mut out = Output::new(&mut buf, 64);
assert!(out.is_empty());
out.write_str("x")?;
assert!(!out.is_empty());
Source

pub fn remaining(&self) -> usize

The bytes still allowed.

use web_net::Output;

let mut buf = Vec::new();
let mut out = Output::new(&mut buf, 10);
out.write_str("abc")?;
assert_eq!(out.remaining(), 7);

Trait Implementations§

Source§

impl Debug for Output<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for Output<'a>

§

impl<'a> Forget for Output<'a>

§

impl<'a> Freeze for Output<'a>

§

impl<'a> Move for Output<'a>

§

impl<'a> RefUnwindSafe for Output<'a>

§

impl<'a> Send for Output<'a>

§

impl<'a> Sync for Output<'a>

§

impl<'a> Unpin for Output<'a>

§

impl<'a> UnsafeUnpin for Output<'a>

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.