1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Library for custom implementations of serialize/deserialize.

use std::result::Result;
use std::sync::Arc;

use serde::de::Error;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
use sync::Mutex;

/// Serialize data `T` inside an `Arc<Mutex<T>>`. `T` must be serializable.
///
/// NOTE: This does not validate already serialized Mutexes and data. If multiple structs contain a
/// clone of the Arc, and they are all being serialized, this will result in the same data being
/// serialized, once per clone.
pub fn serialize_arc_mutex<S: Serializer, T: Serialize + ?Sized>(
    item: &Arc<Mutex<T>>,
    serializer: S,
) -> Result<S::Ok, S::Error> {
    let lock = item.lock();
    serde::Serialize::serialize(&*lock, serializer)
}

/// Serialize data T inside arrays as a seq instead of a tuple. T must be serializable.
/// When deserializing, an array size validation is required to transform the slice to an array.
///
/// This approach is used to go around serde's limitation of serializable array sizes.
pub fn serialize_arr<S, T: Sized + Serialize, const SIZE: usize>(
    data: &[T; SIZE],
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serde::Serialize::serialize(&data[..], serializer)
}

/// Deserialize sequence of T into an Array of SIZE == Size of sequence.
/// This function is a workaround to the serde limitation on array size (32) deserialization.
pub fn deserialize_seq_to_arr<
    'de,
    D,
    T: Sized + Deserialize<'de> + std::fmt::Debug,
    const SIZE: usize,
>(
    deserializer: D,
) -> Result<[T; SIZE], D::Error>
where
    D: Deserializer<'de>,
{
    let vals_vec: Vec<T> = serde::Deserialize::deserialize(deserializer)?;
    let vals_arr: [T; SIZE] = vals_vec.try_into().map_err(|_| {
        <D as Deserializer>::Error::custom("failed to convert vector to array while deserializing")
    })?;
    Ok(vals_arr)
}

pub fn serialize_map_as_kv_vec<
    'se,
    MapKeyType: 'se + Serialize,
    MapValType: 'se + Serialize,
    MapType: std::iter::IntoIterator<Item = (&'se MapKeyType, &'se MapValType)>,
    S,
>(
    map: MapType,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let kv_vec: Vec<(&MapKeyType, &MapValType)> = map.into_iter().collect();
    serde::Serialize::serialize(&kv_vec, serializer)
}

pub fn deserialize_map_from_kv_vec<
    'de,
    MapKeyType: Deserialize<'de>,
    MapValType: Deserialize<'de>,
    MapType: std::iter::FromIterator<(MapKeyType, MapValType)>,
    D,
>(
    deserializer: D,
) -> Result<MapType, D::Error>
where
    D: Deserializer<'de>,
{
    let kv_vec: Vec<(MapKeyType, MapValType)> = serde::Deserialize::deserialize(deserializer)?;
    Ok(MapType::from_iter(kv_vec))
}