iceoryx2-cal 0.9.0

iceoryx2: [internal] high-level traits and implementations that represents OS primitives in an exchangeable fashion
Documentation
// Copyright (c) 2025 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use alloc::vec::Vec;

use iceoryx2_log::fail;

use crate::serialize::Serialize;

use super::{DeserializeError, SerializeError};

#[derive(Debug)]
pub struct Postcard {}

impl Serialize for Postcard {
    fn serialize<T: serde::Serialize>(value: &T) -> Result<Vec<u8>, super::SerializeError> {
        match postcard::to_allocvec(value) {
            Ok(vec) => Ok(vec),
            Err(e) => {
                fail!(
                    from "Postcard::serialize",
                    with SerializeError::InternalError,
                    "Failed to serialize object: {e}"
                );
            }
        }
    }

    fn deserialize<T: serde::de::DeserializeOwned>(
        bytes: &[u8],
    ) -> Result<T, super::DeserializeError> {
        match postcard::from_bytes(bytes) {
            Ok(obj) => Ok(obj),
            Err(e) => {
                fail!(
                    from "Postcard::deserialize",
                    with DeserializeError::InternalError,
                    "Failed to deserialize object: {e}"
                );
            }
        }
    }
}