Skip to main content

Serialize

Trait Serialize 

Source
pub trait Serialize {
    // Required method
    fn serialize<S>(
        &self,
        name: &str,
        serializer: &mut S,
    ) -> Result<(), S::Error>
       where S: Serializer;
}
Expand description

Trait for types that can be serialized.

This trait should be implemented (or derived) for any type that needs to be serialized. The implementation defines how the type should be written to a serializer.

§Derive Macro

The easiest way to implement this trait is using the derive macro (requires derive feature):

use osal_rs_serde::Serialize;

#[derive(Serialize)]
struct Config {
    id: u32,
    enabled: bool,
    timeout: Option<u16>,
}

§Manual Implementation

For custom serialization logic or types not supported by the derive macro:

use osal_rs_serde::{Serialize, Serializer};

struct Point {
    x: i32,
    y: i32,
}

impl Serialize for Point {
    fn serialize<S: Serializer>(&self, serializer: &mut S) -> core::result::Result<(), S::Error> {
        serializer.serialize_i32("x", self.x)?;
        serializer.serialize_i32("y", self.y)?;
        Ok(())
    }
}

§Built-in Implementations

This trait is already implemented for:

  • All primitive types (bool, u8-u128, i8-i128, f32, f64)
  • Arrays [T; N] where T: Serialize
  • Tuples (T1, T2) and (T1, T2, T3) where all T: Serialize
  • Option<T> where T: Serialize
  • Vec<T> where T: Serialize (requires alloc)
  • String and &str (requires alloc for String)

Required Methods§

Source

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Serialize this value using the given serializer.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl Serialize for &str

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for String

Available on crate feature alloc only.
Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for bool

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for f32

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for f64

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for i8

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for i16

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for i32

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for i64

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for i128

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for u8

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for u16

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for u32

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for u64

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl Serialize for u128

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl<T1: Serialize, T2: Serialize, T3: Serialize> Serialize for (T1, T2, T3)

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl<T1: Serialize, T2: Serialize> Serialize for (T1, T2)

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl<T: Serialize, const N: usize> Serialize for [T; N]

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Source§

impl<T: Serialize> Serialize for Option<T>

Source§

fn serialize<S>(&self, name: &str, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,

Implementors§