Skip to content

Commit

Permalink
👽️ zv,zd: Drop all use of top-level deserialization functions
Browse files Browse the repository at this point in the history
Use `serialized::Data` API instead. These top-level functions shall be
dropped in a following commit.
  • Loading branch information
zeenix committed Oct 27, 2023
1 parent 4193d94 commit 4cf9a55
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 220 deletions.
18 changes: 9 additions & 9 deletions zvariant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Serialization and deserialization is achieved through the [toplevel functions]:

```rust
use std::collections::HashMap;
use zvariant::{EncodingContext as Context, from_slice, to_bytes, Type};
use zvariant::{EncodingContext as Context, to_bytes, Type};
use serde::{Deserialize, Serialize};
use byteorder::LE;

Expand All @@ -30,32 +30,32 @@ let ctxt = Context::<LE>::new_dbus(0);

// i16
let encoded = to_bytes(ctxt, &42i16).unwrap();
let decoded: i16 = from_slice(&encoded, ctxt).unwrap().0;
let decoded: i16 = encoded.deserialize().unwrap().0;
assert_eq!(decoded, 42);

// strings
let encoded = to_bytes(ctxt, &"hello").unwrap();
let decoded: &str = from_slice(&encoded, ctxt).unwrap().0;
let decoded: &str = encoded.deserialize().unwrap().0;
assert_eq!(decoded, "hello");

// tuples
let t = ("hello", 42i32, true);
let encoded = to_bytes(ctxt, &t).unwrap();
let decoded: (&str, i32, bool) = from_slice(&encoded, ctxt).unwrap().0;
let decoded: (&str, i32, bool) = encoded.deserialize().unwrap().0;
assert_eq!(decoded, t);

// Vec
let v = vec!["hello", "world!"];
let encoded = to_bytes(ctxt, &v).unwrap();
let decoded: Vec<&str> = from_slice(&encoded, ctxt).unwrap().0;
let decoded: Vec<&str> = encoded.deserialize().unwrap().0;
assert_eq!(decoded, v);

// Dictionary
let mut map: HashMap<i64, &str> = HashMap::new();
map.insert(1, "123");
map.insert(2, "456");
let encoded = to_bytes(ctxt, &map).unwrap();
let decoded: HashMap<i64, &str> = from_slice(&encoded, ctxt).unwrap().0;
let decoded: HashMap<i64, &str> = encoded.deserialize().unwrap().0;
assert_eq!(decoded[&1], "123");
assert_eq!(decoded[&2], "456");

Expand All @@ -75,7 +75,7 @@ let s = Struct {
};
let ctxt = Context::<LE>::new_dbus(0);
let encoded = to_bytes(ctxt, &s).unwrap();
let decoded: Struct = from_slice(&encoded, ctxt).unwrap().0;
let decoded: Struct = encoded.deserialize().unwrap().0;
assert_eq!(decoded, s);

// It can handle enums too, just that all variants must have the same number and types of fields.
Expand All @@ -98,7 +98,7 @@ let e = Enum::Variant3 {
f3: "hello",
};
let encoded = to_bytes(ctxt, &e).unwrap();
let decoded: Enum = from_slice(&encoded, ctxt).unwrap().0;
let decoded: Enum = encoded.deserialize().unwrap().0;
assert_eq!(decoded, e);

#[derive(Deserialize, Serialize, Type, PartialEq, Debug)]
Expand All @@ -112,7 +112,7 @@ enum UnitEnum {

assert_eq!(UnitEnum::signature(), "y");
let encoded = to_bytes(ctxt, &UnitEnum::Variant2).unwrap();
let e: UnitEnum = from_slice(&encoded, ctxt).unwrap().0;
let e: UnitEnum = encoded.deserialize().unwrap().0;
assert_eq!(e, UnitEnum::Variant2);

// Unit enums can also be (de)serialized as strings.
Expand Down
28 changes: 10 additions & 18 deletions zvariant/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use std::collections::HashMap;

use criterion::{black_box, criterion_group, criterion_main, Criterion};

use zvariant::{
from_slice_for_signature, to_bytes_for_signature, EncodingContext as Context, Type, Value,
};
use zvariant::{to_bytes_for_signature, EncodingContext as Context, Type, Value};

fn fixed_size_array(c: &mut Criterion) {
let ay = vec![77u8; 100_000];
Expand All @@ -20,9 +18,9 @@ fn fixed_size_array(c: &mut Criterion) {
let enc = to_bytes_for_signature(ctxt, &signature, &ay).unwrap();
c.bench_function("byte_array_de", |b| {
b.iter(|| {
let _: (Vec<u8>, _) =
from_slice_for_signature(black_box(&enc), black_box(ctxt), black_box(&signature))
.unwrap();
let _: (Vec<u8>, _) = enc
.deserialize_for_signature(black_box(&signature))
.unwrap();
})
});
}
Expand Down Expand Up @@ -87,12 +85,9 @@ fn big_array_ser_and_de(c: &mut Criterion) {
let encoded = to_bytes_for_signature(ctxt, &signature, &element).unwrap();
c.bench_function("big_array_de_dbus", |b| {
b.iter(|| {
let (s, _): (ZVStruct, _) = from_slice_for_signature(
black_box(&encoded),
black_box(ctxt),
black_box(&signature),
)
.unwrap();
let (s, _): (ZVStruct, _) = encoded
.deserialize_for_signature(black_box(&signature))
.unwrap();
black_box(s);
})
});
Expand All @@ -117,12 +112,9 @@ fn big_array_ser_and_de(c: &mut Criterion) {
let encoded = to_bytes_for_signature(ctxt, &signature, &element).unwrap();
c.bench_function("big_array_de_gvariant", |b| {
b.iter(|| {
let (s, _): (ZVStruct, _) = from_slice_for_signature(
black_box(&encoded),
black_box(ctxt),
black_box(&signature),
)
.unwrap();
let (s, _): (ZVStruct, _) = encoded
.deserialize_for_signature(black_box(&signature))
.unwrap();
black_box(s);
})
});
Expand Down
7 changes: 4 additions & 3 deletions zvariant/fuzz/fuzz_targets/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use byteorder::ByteOrder;
use zvariant::{from_slice, to_bytes, EncodingContext as Context, Value};
use zvariant::{serialized::Data, to_bytes, EncodingContext as Context, Value};

pub fn fuzz_for_context<B: ByteOrder>(data: &[u8], ctx: Context<B>) {
if let Ok((decoded, _)) = from_slice::<_, Value>(data, ctx) {
pub fn fuzz_for_context<B: ByteOrder>(bytes: &[u8], ctx: Context<B>) {
let data = Data::new(bytes, ctx);
if let Ok((decoded, _)) = data.deserialize::<Value>() {
to_bytes(ctx, &decoded).unwrap();
}
}
4 changes: 2 additions & 2 deletions zvariant/src/deserialize_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use crate::{Signature, Type, Value};
/// generic [`Value`] and instead use this wrapper.
///
/// ```
/// # use zvariant::{to_bytes, EncodingContext, DeserializeValue, SerializeValue, from_slice};
/// # use zvariant::{to_bytes, EncodingContext, DeserializeValue, SerializeValue};
/// #
/// # let ctxt = EncodingContext::<byteorder::LE>::new_dbus(0);
/// # let array = [0, 1, 2];
/// # let v = SerializeValue(&array);
/// # let encoded = to_bytes(ctxt, &v).unwrap();
/// let decoded: DeserializeValue<[u8; 3]> = from_slice(&encoded, ctxt).unwrap().0;
/// let decoded: DeserializeValue<[u8; 3]> = encoded.deserialize().unwrap().0;
/// # assert_eq!(decoded.0, array);
/// ```
///
Expand Down
6 changes: 3 additions & 3 deletions zvariant/src/encoding_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ impl std::fmt::Display for EncodingFormat {
/// use byteorder::LE;
///
/// use zvariant::EncodingContext as Context;
/// use zvariant::{from_slice, to_bytes};
/// use zvariant::to_bytes;
///
/// let str_vec = vec!["Hello", "World"];
/// let ctxt = Context::<LE>::new_dbus(0);
/// let encoded = to_bytes(ctxt, &str_vec).unwrap();
///
/// // Let's decode the 2nd element of the array only
/// let ctxt = Context::<LE>::new_dbus(14);
/// let decoded: &str = from_slice(&encoded[14..], ctxt).unwrap().0;
/// let slice = encoded.slice(14..);
/// let decoded: &str = slice.deserialize().unwrap().0;
/// assert_eq!(decoded, "World");
/// ```
///
Expand Down
Loading

0 comments on commit 4cf9a55

Please sign in to comment.