Skip to main content

PreparedJson

Struct PreparedJson 

Source
pub struct PreparedJson<T> { /* private fields */ }
Expand description

A JSON response body the operation encoded itself.

Json<T> is encoded after the operation has returned, so the value it wraps has to own everything it prints. An operation that reads from a lock guard, an arena, or a shared corpus therefore has to clone every string it wants to include, and those clones are freed again a few microseconds later once the body has been written.

PreparedJson<T> moves the encode step inside the operation, where those borrows are still alive, and carries the finished bytes to the transport untouched. A listing operation can build a borrowed view over the store, encode it while it still holds the read guard, and never allocate an owned mirror of the data at all.

The type parameter carries the documented schema and nothing else: the operation still advertises T in OpenAPI, MCP, and compatibility analysis, exactly as Json<T> would.

§Contract

The framework does not re-parse the bytes, so it cannot check them against T. The operation asserts that the body it encoded is a valid instance of the schema it declares. Use PreparedJson::encode with a value whose serialized shape matches T; PreparedJson::from_bytes hands the same obligation to the caller for bytes produced some other way.

§Examples

use blazingly_core::{ApiSchema, PreparedJson, SchemaKind, TypeDescriptor};
use serde::Serialize;

#[derive(Serialize)]
struct BorrowedPage<'store> {
    items: Vec<&'store str>,
}

let store = vec![String::from("first"), String::from("second")];
let view = BorrowedPage {
    items: store.iter().map(String::as_str).collect(),
};
let body = PreparedJson::<Page>::encode(&view).expect("the view encodes");
assert_eq!(body.as_bytes(), br#"{"items":["first","second"]}"#);

Implementations§

Source§

impl<T> PreparedJson<T>

Source

pub const fn from_bytes(body: Vec<u8>) -> Self

Adopts bytes the caller has already encoded.

The bytes are sent verbatim; see the type-level contract note.

Source

pub fn as_bytes(&self) -> &[u8]

Source

pub fn into_bytes(self) -> Vec<u8>

Source

pub const fn len(&self) -> usize

Source

pub const fn is_empty(&self) -> bool

Source

pub fn encode<V>(value: &V) -> Result<Self, Error>
where V: Serialize + ?Sized,

Encodes value into the response body now.

value may borrow from anything alive at the call site, which is the whole point: it is encoded before the borrow ends.

§Errors

Returns the blazingly_json failure when value cannot be encoded, for instance because a map key is not a string.

Trait Implementations§

Source§

impl<T: ApiSchema> ApiSchema for PreparedJson<T>

Source§

fn type_descriptor() -> TypeDescriptor

Source§

fn validate_input(&self) -> Result<(), ValidationErrors>

Validates a decoded operation argument. Read more
Source§

impl<T> Debug for PreparedJson<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for PreparedJson<T>

§

impl<T> RefUnwindSafe for PreparedJson<T>

§

impl<T> Send for PreparedJson<T>

§

impl<T> Sync for PreparedJson<T>

§

impl<T> Unpin for PreparedJson<T>

§

impl<T> UnsafeUnpin for PreparedJson<T>

§

impl<T> UnwindSafe for PreparedJson<T>

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> BackgroundExt for T

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> ResponseExt for T

Source§

fn header( self, name: impl Into<String>, value: impl Into<String>, ) -> WithHeaders<Self>

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