Skip to content

Commit

Permalink
add to_lazy_value
Browse files Browse the repository at this point in the history
  • Loading branch information
minghuaw committed Oct 29, 2024
1 parent 4518adb commit a7fe09f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion serde_amqp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_amqp"
version = "0.13.0"
version = "0.13.1"
edition = "2021"
description = "A serde implementation of AMQP1.0 protocol."
license = "MIT/Apache-2.0"
Expand Down
4 changes: 4 additions & 0 deletions serde_amqp/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.13.1

1. Added `to_lazy_value`

## 0.13.0

### Breaking
Expand Down
9 changes: 9 additions & 0 deletions serde_amqp/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ use crate::{
__constants::LAZY_VALUE,
};

/// Serialize a value into a [`LazyValue`].
pub fn to_lazy_value<T>(value: &T) -> Result<LazyValue, Error>
where
T: Serialize,
{
let bytes = crate::to_vec(value)?;
Ok(LazyValue(Bytes::from(bytes)))
}

/// A lazy value.
///
/// This is a thin wrapper around a [`Bytes`] value that can be accessed via
Expand Down
23 changes: 23 additions & 0 deletions serde_amqp/src/size_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ mod tests {
use serde_bytes::ByteBuf;

use crate::{
lazy::to_lazy_value,
primitives::{Array, Dec128, Dec32, Dec64, Symbol, Timestamp, Uuid},
to_vec,
};
Expand Down Expand Up @@ -1311,4 +1312,26 @@ mod tests {
let size_result = serialized_size(&value);
assert!(size_result.is_ok());
}

#[test]
fn serialized_size_of_lazy_value_match_with_value() {
use crate::{described::Described, descriptor::Descriptor, primitives::*, Value};

let timestamp = Timestamp::from_milliseconds(12345);
let mut list = List::new();
list.push(Value::Timestamp(timestamp));

let described = Described {
descriptor: Descriptor::Code(0x73),
value: Value::List(list),
};

let value = Value::Described(Box::new(described));
let lazy_value = to_lazy_value(&value).unwrap();

let value_size = serialized_size(&value).unwrap();
let lazy_value_size = serialized_size(&lazy_value).unwrap();

assert_eq!(value_size, lazy_value_size);
}
}

0 comments on commit a7fe09f

Please sign in to comment.