Skip to main content

TypedWriter

Trait TypedWriter 

Source
pub trait TypedWriter: Writer {
    // Provided methods
    fn write_as<T: Serialize>(
        &mut self,
        to: &Path,
        data: &T,
    ) -> Result<Path, Error> { ... }
    fn write_json(&mut self, to: &Path, data: Value) -> Result<Path, Error> { ... }
    fn write_typed<T: Serialize>(
        &mut self,
        to: &Path,
        data: &T,
    ) -> Result<Path, Error> { ... }
}
Expand description

Extension trait for typed writes.

This trait is automatically implemented for all Writer implementations. It provides convenience methods for writing Rust types directly.

§Example

use structfs_serde_store::TypedWriter;
use serde::Serialize;

#[derive(Serialize)]
struct User {
    name: String,
    email: String,
}

fn create_user(store: &mut dyn Writer, user: &User) -> Result<Path, Error> {
    store.write_as(&path!("users/new"), user)
}

Provided Methods§

Source

fn write_as<T: Serialize>(&mut self, to: &Path, data: &T) -> Result<Path, Error>

Serialize a Rust type and write it to the store.

This method:

  1. Serializes the data to a Value
  2. Wraps it in a Record::Parsed
  3. Writes it to the store
Source

fn write_json(&mut self, to: &Path, data: Value) -> Result<Path, Error>

Write a serde_json::Value to the store.

Convenience method for dynamic JSON data.

Source

fn write_typed<T: Serialize>( &mut self, to: &Path, data: &T, ) -> Result<Path, Error>

Codec-free typed write — the mirror of TypedReader::read_typed.

Identical to TypedWriter::write_as; provided so read/write call sites pair up by name.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<W: Writer + ?Sized> TypedWriter for W