Context

Struct Context 

Source
pub struct Context;
Expand description

A context for reading and writing values.

This is created by calling Context::new, and is used to read values from the input and write values to the output.

Implementations§

Source§

impl Context

Source

pub fn log(&mut self, message: &str)

Log message.

Source§

impl Context

Source

pub fn write_bool(&mut self, value: bool) -> Result<(), Error>

Write a boolean value.

Source

pub fn write_null(&mut self) -> Result<(), Error>

Write a null value.

Source

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

Write an i32 value.

Source

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

Write a f64 value.

Source

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

Write a UTF-8 string value.

Source

pub fn write_interned_utf8_str( &mut self, id: InternedStringId, ) -> Result<(), Error>

Write an interned UTF-8 string value.

Source

pub fn write_object<F: FnOnce(&mut Self) -> Result<(), Error>>( &mut self, f: F, len: usize, ) -> Result<(), Error>

Write an object. You must provide the exact number of key-value pairs you will write.

Source

pub fn write_array<F: FnOnce(&mut Self) -> Result<(), Error>>( &mut self, f: F, len: usize, ) -> Result<(), Error>

Write an array. You must provide the exact number of values you will write.

Source

pub fn finalize_output_and_return(self) -> Result<Value, Error>

Finalize the output and return the serialized value as a serde_json::Value. This is only available in non-Wasm targets, and therefore only recommended for use in tests.

Source§

impl Context

Source

pub fn new() -> Self

Create a new context.

This is only intended to be invoked when compiled to a Wasm target.

§Panics

This will panic if called from a non-Wasm environment.

Source

pub fn new_with_input(input: Value) -> Self

Create a new context from a JSON value, which will be the top-level value of the input.

This is only available when compiled to a non-Wasm target, for usage in unit tests.

Source

pub fn input_get(&self) -> Result<Value, ContextError>

Get the top-level value of the input.

Source

pub fn intern_utf8_str(&self, s: &str) -> InternedStringId

Intern a string. This can lead to performance gains if you are using the same string multiple times, as it saves unnecessary string copies. For example, if you are reading the same property from multiple objects, or serializing the same key on an object, you can intern the string once and reuse it.

Trait Implementations§

Source§

impl Default for Context

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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 = 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.