Coverage Report

Created: 2024-10-16 07:58

/rust/registry/src/index.crates.io-6f17d22bba15001f/rkyv-0.7.44/src/with/std.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::{
2
    collections::util::Entry,
3
    ser::{ScratchSpace, Serializer},
4
    string::{ArchivedString, StringResolver},
5
    time::ArchivedDuration,
6
    vec::{ArchivedVec, VecResolver},
7
    with::{
8
        ArchiveWith, AsString, AsStringError, AsVec, DeserializeWith, Immutable, Lock, LockError,
9
        SerializeWith, UnixTimestamp, UnixTimestampError,
10
    },
11
    Archive, Deserialize, Fallible, Serialize, SerializeUnsized,
12
};
13
use core::{hash::Hash, str::FromStr};
14
use std::{
15
    collections::{HashMap, HashSet},
16
    ffi::OsString,
17
    path::PathBuf,
18
    sync::{Mutex, RwLock},
19
    time::{SystemTime, UNIX_EPOCH},
20
};
21
22
// AsString
23
24
impl ArchiveWith<OsString> for AsString {
25
    type Archived = ArchivedString;
26
    type Resolver = StringResolver;
27
28
    #[inline]
29
0
    unsafe fn resolve_with(
30
0
        field: &OsString,
31
0
        pos: usize,
32
0
        resolver: Self::Resolver,
33
0
        out: *mut Self::Archived,
34
0
    ) {
35
0
        // It's safe to unwrap here because if the OsString wasn't valid UTF-8 it would have failed
36
0
        // to serialize
37
0
        ArchivedString::resolve_from_str(field.to_str().unwrap(), pos, resolver, out);
38
0
    }
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::ArchiveWith<std::ffi::os_str::OsString>>::resolve_with
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::ArchiveWith<std::ffi::os_str::OsString>>::resolve_with
39
}
40
41
impl<S: Fallible + ?Sized> SerializeWith<OsString, S> for AsString
42
where
43
    S::Error: From<AsStringError>,
44
    str: SerializeUnsized<S>,
45
{
46
    #[inline]
47
0
    fn serialize_with(field: &OsString, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
48
0
        ArchivedString::serialize_from_str(
49
0
            field.to_str().ok_or(AsStringError::InvalidUTF8)?,
50
0
            serializer,
51
        )
52
0
    }
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::SerializeWith<std::ffi::os_str::OsString, _>>::serialize_with
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::SerializeWith<std::ffi::os_str::OsString, _>>::serialize_with
53
}
54
55
impl<D: Fallible + ?Sized> DeserializeWith<ArchivedString, OsString, D> for AsString {
56
    #[inline]
57
0
    fn deserialize_with(field: &ArchivedString, _: &mut D) -> Result<OsString, D::Error> {
58
0
        Ok(OsString::from_str(field.as_str()).unwrap())
59
0
    }
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::DeserializeWith<rkyv::string::ArchivedString, std::ffi::os_str::OsString, _>>::deserialize_with
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::DeserializeWith<rkyv::string::ArchivedString, std::ffi::os_str::OsString, _>>::deserialize_with
60
}
61
62
impl ArchiveWith<PathBuf> for AsString {
63
    type Archived = ArchivedString;
64
    type Resolver = StringResolver;
65
66
    #[inline]
67
0
    unsafe fn resolve_with(
68
0
        field: &PathBuf,
69
0
        pos: usize,
70
0
        resolver: Self::Resolver,
71
0
        out: *mut Self::Archived,
72
0
    ) {
73
0
        // It's safe to unwrap here because if the OsString wasn't valid UTF-8 it would have failed
74
0
        // to serialize
75
0
        ArchivedString::resolve_from_str(field.to_str().unwrap(), pos, resolver, out);
76
0
    }
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::ArchiveWith<std::path::PathBuf>>::resolve_with
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::ArchiveWith<std::path::PathBuf>>::resolve_with
77
}
78
79
impl<S: Fallible + ?Sized> SerializeWith<PathBuf, S> for AsString
80
where
81
    S::Error: From<AsStringError>,
82
    str: SerializeUnsized<S>,
83
{
84
    #[inline]
85
0
    fn serialize_with(field: &PathBuf, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
86
0
        ArchivedString::serialize_from_str(
87
0
            field.to_str().ok_or(AsStringError::InvalidUTF8)?,
88
0
            serializer,
89
        )
90
0
    }
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::SerializeWith<std::path::PathBuf, _>>::serialize_with
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::SerializeWith<std::path::PathBuf, _>>::serialize_with
91
}
92
93
impl<D: Fallible + ?Sized> DeserializeWith<ArchivedString, PathBuf, D> for AsString {
94
    #[inline]
95
0
    fn deserialize_with(field: &ArchivedString, _: &mut D) -> Result<PathBuf, D::Error> {
96
0
        Ok(PathBuf::from_str(field.as_str()).unwrap())
97
0
    }
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::DeserializeWith<rkyv::string::ArchivedString, std::path::PathBuf, _>>::deserialize_with
Unexecuted instantiation: <rkyv::with::AsString as rkyv::with::DeserializeWith<rkyv::string::ArchivedString, std::path::PathBuf, _>>::deserialize_with
98
}
99
100
// Lock
101
102
impl<F: Archive> ArchiveWith<Mutex<F>> for Lock {
103
    type Archived = Immutable<F::Archived>;
104
    type Resolver = F::Resolver;
105
106
    #[inline]
107
0
    unsafe fn resolve_with(
108
0
        field: &Mutex<F>,
109
0
        pos: usize,
110
0
        resolver: Self::Resolver,
111
0
        out: *mut Self::Archived,
112
0
    ) {
113
0
        // Unfortunately, we have to unwrap here because resolve must be infallible
114
0
        //
115
0
        // An alternative would be to only implement ArchiveWith for Arc<Mutex<F>>, copy an Arc into
116
0
        // the resolver, and hold the guard in there as well (as a reference to the internal mutex).
117
0
        // This unfortunately will cause a deadlock if two Arcs to the same Mutex are serialized
118
0
        // before the first is resolved. The compromise is, unfortunately, to just unwrap poison
119
0
        // errors here and document it.
120
0
        field.lock().unwrap().resolve(pos, resolver, out.cast());
121
0
    }
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::ArchiveWith<std::sync::mutex::Mutex<_>>>::resolve_with
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::ArchiveWith<std::sync::mutex::Mutex<_>>>::resolve_with
122
}
123
124
impl<F: Serialize<S>, S: Fallible + ?Sized> SerializeWith<Mutex<F>, S> for Lock
125
where
126
    S::Error: From<LockError>,
127
{
128
    #[inline]
129
0
    fn serialize_with(field: &Mutex<F>, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
130
0
        field
131
0
            .lock()
132
0
            .map_err(|_| LockError::Poisoned)?
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::SerializeWith<std::sync::mutex::Mutex<_>, _>>::serialize_with::{closure#0}
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::SerializeWith<std::sync::mutex::Mutex<_>, _>>::serialize_with::{closure#0}
133
0
            .serialize(serializer)
134
0
    }
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::SerializeWith<std::sync::mutex::Mutex<_>, _>>::serialize_with
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::SerializeWith<std::sync::mutex::Mutex<_>, _>>::serialize_with
135
}
136
137
impl<F, T, D> DeserializeWith<Immutable<F>, Mutex<T>, D> for Lock
138
where
139
    F: Deserialize<T, D>,
140
    D: Fallible + ?Sized,
141
{
142
    #[inline]
143
0
    fn deserialize_with(field: &Immutable<F>, deserializer: &mut D) -> Result<Mutex<T>, D::Error> {
144
0
        Ok(Mutex::new(field.value().deserialize(deserializer)?))
145
0
    }
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::DeserializeWith<rkyv::with::Immutable<_>, std::sync::mutex::Mutex<_>, _>>::deserialize_with
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::DeserializeWith<rkyv::with::Immutable<_>, std::sync::mutex::Mutex<_>, _>>::deserialize_with
146
}
147
148
impl<F: Archive> ArchiveWith<RwLock<F>> for Lock {
149
    type Archived = Immutable<F::Archived>;
150
    type Resolver = F::Resolver;
151
152
    #[inline]
153
0
    unsafe fn resolve_with(
154
0
        field: &RwLock<F>,
155
0
        pos: usize,
156
0
        resolver: Self::Resolver,
157
0
        out: *mut Self::Archived,
158
0
    ) {
159
0
        // Unfortunately, we have to unwrap here because resolve must be infallible
160
0
        //
161
0
        // An alternative would be to only implement ArchiveWith for Arc<Mutex<F>>, copy a an Arc
162
0
        // into the resolver, and hold the guard in there as well (as a reference to the internal
163
0
        // mutex). This unfortunately will cause a deadlock if two Arcs to the same Mutex are
164
0
        // serialized before the first is resolved. The compromise is, unfortunately, to just
165
0
        // unwrap poison errors here and document it.
166
0
        field.read().unwrap().resolve(pos, resolver, out.cast());
167
0
    }
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::ArchiveWith<std::sync::rwlock::RwLock<_>>>::resolve_with
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::ArchiveWith<std::sync::rwlock::RwLock<_>>>::resolve_with
168
}
169
170
impl<F: Serialize<S>, S: Fallible + ?Sized> SerializeWith<RwLock<F>, S> for Lock
171
where
172
    S::Error: From<LockError>,
173
{
174
    #[inline]
175
0
    fn serialize_with(field: &RwLock<F>, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
176
0
        field
177
0
            .read()
178
0
            .map_err(|_| LockError::Poisoned)?
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::SerializeWith<std::sync::rwlock::RwLock<_>, _>>::serialize_with::{closure#0}
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::SerializeWith<std::sync::rwlock::RwLock<_>, _>>::serialize_with::{closure#0}
179
0
            .serialize(serializer)
180
0
    }
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::SerializeWith<std::sync::rwlock::RwLock<_>, _>>::serialize_with
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::SerializeWith<std::sync::rwlock::RwLock<_>, _>>::serialize_with
181
}
182
183
impl<F, T, D> DeserializeWith<Immutable<F>, RwLock<T>, D> for Lock
184
where
185
    F: Deserialize<T, D>,
186
    D: Fallible + ?Sized,
187
{
188
    #[inline]
189
0
    fn deserialize_with(field: &Immutable<F>, deserializer: &mut D) -> Result<RwLock<T>, D::Error> {
190
0
        Ok(RwLock::new(field.value().deserialize(deserializer)?))
191
0
    }
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::DeserializeWith<rkyv::with::Immutable<_>, std::sync::rwlock::RwLock<_>, _>>::deserialize_with
Unexecuted instantiation: <rkyv::with::Lock as rkyv::with::DeserializeWith<rkyv::with::Immutable<_>, std::sync::rwlock::RwLock<_>, _>>::deserialize_with
192
}
193
194
// AsVec
195
196
impl<K: Archive, V: Archive> ArchiveWith<HashMap<K, V>> for AsVec {
197
    type Archived = ArchivedVec<Entry<K::Archived, V::Archived>>;
198
    type Resolver = VecResolver;
199
200
0
    unsafe fn resolve_with(
201
0
        field: &HashMap<K, V>,
202
0
        pos: usize,
203
0
        resolver: Self::Resolver,
204
0
        out: *mut Self::Archived,
205
0
    ) {
206
0
        ArchivedVec::resolve_from_len(field.len(), pos, resolver, out);
207
0
    }
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::ArchiveWith<std::collections::hash::map::HashMap<_, _>>>::resolve_with
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::ArchiveWith<std::collections::hash::map::HashMap<_, _>>>::resolve_with
208
}
209
210
impl<K, V, S> SerializeWith<HashMap<K, V>, S> for AsVec
211
where
212
    K: Serialize<S>,
213
    V: Serialize<S>,
214
    S: ScratchSpace + Serializer + ?Sized,
215
{
216
0
    fn serialize_with(
217
0
        field: &HashMap<K, V>,
218
0
        serializer: &mut S,
219
0
    ) -> Result<Self::Resolver, S::Error> {
220
0
        ArchivedVec::serialize_from_iter(
221
0
            field.iter().map(|(key, value)| Entry { key, value }),
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::SerializeWith<std::collections::hash::map::HashMap<_, _>, _>>::serialize_with::{closure#0}
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::SerializeWith<std::collections::hash::map::HashMap<_, _>, _>>::serialize_with::{closure#0}
222
0
            serializer,
223
0
        )
224
0
    }
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::SerializeWith<std::collections::hash::map::HashMap<_, _>, _>>::serialize_with
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::SerializeWith<std::collections::hash::map::HashMap<_, _>, _>>::serialize_with
225
}
226
227
impl<K, V, D> DeserializeWith<ArchivedVec<Entry<K::Archived, V::Archived>>, HashMap<K, V>, D>
228
    for AsVec
229
where
230
    K: Archive + Hash + Eq,
231
    V: Archive,
232
    K::Archived: Deserialize<K, D>,
233
    V::Archived: Deserialize<V, D>,
234
    D: Fallible + ?Sized,
235
{
236
0
    fn deserialize_with(
237
0
        field: &ArchivedVec<Entry<K::Archived, V::Archived>>,
238
0
        deserializer: &mut D,
239
0
    ) -> Result<HashMap<K, V>, D::Error> {
240
0
        let mut result = HashMap::with_capacity(field.len());
241
0
        for entry in field.iter() {
242
0
            result.insert(
243
0
                entry.key.deserialize(deserializer)?,
244
0
                entry.value.deserialize(deserializer)?,
245
            );
246
        }
247
0
        Ok(result)
248
0
    }
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::DeserializeWith<rkyv::vec::ArchivedVec<rkyv::collections::util::Entry<<_ as rkyv::Archive>::Archived, <_ as rkyv::Archive>::Archived>>, std::collections::hash::map::HashMap<_, _>, _>>::deserialize_with
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::DeserializeWith<rkyv::vec::ArchivedVec<rkyv::collections::util::Entry<<_ as rkyv::Archive>::Archived, <_ as rkyv::Archive>::Archived>>, std::collections::hash::map::HashMap<_, _>, _>>::deserialize_with
249
}
250
251
impl<T: Archive> ArchiveWith<HashSet<T>> for AsVec {
252
    type Archived = ArchivedVec<T::Archived>;
253
    type Resolver = VecResolver;
254
255
0
    unsafe fn resolve_with(
256
0
        field: &HashSet<T>,
257
0
        pos: usize,
258
0
        resolver: Self::Resolver,
259
0
        out: *mut Self::Archived,
260
0
    ) {
261
0
        ArchivedVec::resolve_from_len(field.len(), pos, resolver, out);
262
0
    }
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::ArchiveWith<std::collections::hash::set::HashSet<_>>>::resolve_with
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::ArchiveWith<std::collections::hash::set::HashSet<_>>>::resolve_with
263
}
264
265
impl<T, S> SerializeWith<HashSet<T>, S> for AsVec
266
where
267
    T: Serialize<S>,
268
    S: ScratchSpace + Serializer + ?Sized,
269
{
270
0
    fn serialize_with(field: &HashSet<T>, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
271
0
        ArchivedVec::<T::Archived>::serialize_from_iter::<T, _, _, _>(field.iter(), serializer)
272
0
    }
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::SerializeWith<std::collections::hash::set::HashSet<_>, _>>::serialize_with
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::SerializeWith<std::collections::hash::set::HashSet<_>, _>>::serialize_with
273
}
274
275
impl<T, D> DeserializeWith<ArchivedVec<T::Archived>, HashSet<T>, D> for AsVec
276
where
277
    T: Archive + Hash + Eq,
278
    T::Archived: Deserialize<T, D>,
279
    D: Fallible + ?Sized,
280
{
281
0
    fn deserialize_with(
282
0
        field: &ArchivedVec<T::Archived>,
283
0
        deserializer: &mut D,
284
0
    ) -> Result<HashSet<T>, D::Error> {
285
0
        let mut result = HashSet::with_capacity(field.len());
286
0
        for key in field.iter() {
287
0
            result.insert(key.deserialize(deserializer)?);
288
        }
289
0
        Ok(result)
290
0
    }
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::DeserializeWith<rkyv::vec::ArchivedVec<<_ as rkyv::Archive>::Archived>, std::collections::hash::set::HashSet<_>, _>>::deserialize_with
Unexecuted instantiation: <rkyv::with::AsVec as rkyv::with::DeserializeWith<rkyv::vec::ArchivedVec<<_ as rkyv::Archive>::Archived>, std::collections::hash::set::HashSet<_>, _>>::deserialize_with
291
}
292
293
// UnixTimestamp
294
295
impl ArchiveWith<SystemTime> for UnixTimestamp {
296
    type Archived = ArchivedDuration;
297
    type Resolver = ();
298
299
    #[inline]
300
0
    unsafe fn resolve_with(
301
0
        field: &SystemTime,
302
0
        pos: usize,
303
0
        resolver: Self::Resolver,
304
0
        out: *mut Self::Archived,
305
0
    ) {
306
0
        // We already checked the duration during serialize_with
307
0
        let duration = field.duration_since(UNIX_EPOCH).unwrap();
308
0
        Archive::resolve(&duration, pos, resolver, out);
309
0
    }
Unexecuted instantiation: <rkyv::with::UnixTimestamp as rkyv::with::ArchiveWith<std::time::SystemTime>>::resolve_with
Unexecuted instantiation: <rkyv::with::UnixTimestamp as rkyv::with::ArchiveWith<std::time::SystemTime>>::resolve_with
310
}
311
312
impl<S: Fallible + ?Sized> SerializeWith<SystemTime, S> for UnixTimestamp
313
where
314
    S::Error: From<UnixTimestampError>,
315
{
316
0
    fn serialize_with(field: &SystemTime, _: &mut S) -> Result<Self::Resolver, S::Error> {
317
0
        field
318
0
            .duration_since(UNIX_EPOCH)
319
0
            .map_err(|_| UnixTimestampError::TimeBeforeUnixEpoch)?;
Unexecuted instantiation: <rkyv::with::UnixTimestamp as rkyv::with::SerializeWith<std::time::SystemTime, _>>::serialize_with::{closure#0}
Unexecuted instantiation: <rkyv::with::UnixTimestamp as rkyv::with::SerializeWith<std::time::SystemTime, _>>::serialize_with::{closure#0}
320
0
        Ok(())
321
0
    }
Unexecuted instantiation: <rkyv::with::UnixTimestamp as rkyv::with::SerializeWith<std::time::SystemTime, _>>::serialize_with
Unexecuted instantiation: <rkyv::with::UnixTimestamp as rkyv::with::SerializeWith<std::time::SystemTime, _>>::serialize_with
322
}
323
324
impl<D: Fallible + ?Sized> DeserializeWith<ArchivedDuration, SystemTime, D> for UnixTimestamp {
325
0
    fn deserialize_with(field: &ArchivedDuration, _: &mut D) -> Result<SystemTime, D::Error> {
326
0
        Ok(UNIX_EPOCH + (*field).into())
327
0
    }
Unexecuted instantiation: <rkyv::with::UnixTimestamp as rkyv::with::DeserializeWith<rkyv::time::ArchivedDuration, std::time::SystemTime, _>>::deserialize_with
Unexecuted instantiation: <rkyv::with::UnixTimestamp as rkyv::with::DeserializeWith<rkyv::time::ArchivedDuration, std::time::SystemTime, _>>::deserialize_with
328
}