• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vigna / epserde-rs / 18009770154

25 Sep 2025 01:52PM UTC coverage: 41.837% (-0.7%) from 42.508%
18009770154

push

github

zommiommy
ignore Readme on no_std

633 of 1513 relevant lines covered (41.84%)

264.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/epserde/src/impls/string.rs
1
/*
2
 * SPDX-FileCopyrightText: 2023 Inria
3
 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
4
 *
5
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
6
 */
7

8
//! Implementations for strings.
9
//!
10
//! All string types have the same serialized type, `Box<str>`, and
11
//! the same deserialized type, `&str`. Thus, you can serialize a `String` and
12
//! fully deserialize it as `Box<str>`.
13
//!
14
//! Similarly to the case of [slices](crate::impls::slice), there is
15
//! a convenience [`SerInner`] implementation for `&str` that
16
//! serializes it as `Box<str>`.
17

18
use crate::prelude::*;
19
use core::hash::Hash;
20
use deser::*;
21
use ser::*;
22

23
#[cfg(not(feature = "std"))]
24
use alloc::boxed::Box;
25

26
impl TypeHash for str {
27
    fn type_hash(hasher: &mut impl core::hash::Hasher) {
×
28
        "str".hash(hasher);
×
29
    }
30
}
31

32
impl AlignHash for str {
33
    fn align_hash(_hasher: &mut impl core::hash::Hasher, _offset_of: &mut usize) {}
×
34
}
35

36
unsafe impl CopyType for String {
37
    type Copy = Deep;
38
}
39

40
#[cfg(not(feature = "std"))]
41
use alloc::string::String;
42

43
impl TypeHash for String {
44
    fn type_hash(hasher: &mut impl core::hash::Hasher) {
13✔
45
        "String".hash(hasher);
39✔
46
    }
47
}
48

49
impl AlignHash for String {
50
    fn align_hash(_hasher: &mut impl core::hash::Hasher, _offset_of: &mut usize) {}
26✔
51
}
52

53
impl SerInner for String {
54
    type SerType = Box<str>;
55
    const IS_ZERO_COPY: bool = false;
56
    const ZERO_COPY_MISMATCH: bool = false;
57

58
    unsafe fn _ser_inner(&self, backend: &mut impl WriteWithNames) -> ser::Result<()> {
337✔
59
        ser_slice_zero(backend, self.as_bytes())
1,011✔
60
    }
61
}
62

63
impl DeserInner for String {
64
    unsafe fn _deser_full_inner(backend: &mut impl ReadWithPos) -> deser::Result<Self> {
669✔
65
        let slice = unsafe { deser_full_vec_zero(backend) }?;
2,007✔
66
        Ok(String::from_utf8(slice).unwrap())
×
67
    }
68

69
    type DeserType<'a> = &'a str;
70

71
    unsafe fn _deser_eps_inner<'a>(
30✔
72
        backend: &mut SliceWithPos<'a>,
73
    ) -> deser::Result<Self::DeserType<'a>> {
74
        let slice = unsafe { deser_eps_slice_zero(backend) }?;
90✔
75
        // SAFETY: Actually this is unsafe if the data we read is not valid UTF-8
76
        Ok({
×
77
            unsafe {
×
78
                #[allow(clippy::transmute_bytes_to_str)]
×
79
                core::mem::transmute::<&'_ [u8], &'_ str>(slice)
×
80
            }
81
        })
82
    }
83
}
84

85
unsafe impl CopyType for Box<str> {
86
    type Copy = Deep;
87
}
88

89
impl TypeHash for Box<str> {
90
    fn type_hash(hasher: &mut impl core::hash::Hasher) {
25✔
91
        "Box<str>".hash(hasher);
75✔
92
    }
93
}
94

95
impl AlignHash for Box<str> {
96
    fn align_hash(_hasher: &mut impl core::hash::Hasher, _offset_of: &mut usize) {}
50✔
97
}
98

99
impl SerInner for Box<str> {
100
    type SerType = Self;
101
    // Box<[$ty]> can, but Vec<Box<[$ty]>> cannot!
102
    const IS_ZERO_COPY: bool = false;
103
    const ZERO_COPY_MISMATCH: bool = false;
104

105
    unsafe fn _ser_inner(&self, backend: &mut impl WriteWithNames) -> ser::Result<()> {
2✔
106
        ser_slice_zero(backend, self.as_bytes())
6✔
107
    }
108
}
109

110
impl DeserInner for Box<str> {
111
    #[inline(always)]
112
    unsafe fn _deser_full_inner(backend: &mut impl ReadWithPos) -> deser::Result<Self> {
4✔
113
        Ok(unsafe { String::_deser_full_inner(backend) }?.into_boxed_str())
12✔
114
    }
115

116
    type DeserType<'a> = &'a str;
117

118
    #[inline(always)]
119
    unsafe fn _deser_eps_inner<'a>(
6✔
120
        backend: &mut SliceWithPos<'a>,
121
    ) -> deser::Result<Self::DeserType<'a>> {
122
        unsafe { String::_deser_eps_inner(backend) }
12✔
123
    }
124
}
125

126
impl SerInner for &str {
127
    type SerType = Box<str>;
128
    const IS_ZERO_COPY: bool = false;
129
    const ZERO_COPY_MISMATCH: bool = false;
130

131
    unsafe fn _ser_inner(&self, backend: &mut impl WriteWithNames) -> ser::Result<()> {
2✔
132
        ser_slice_zero(backend, self.as_bytes())
6✔
133
    }
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc