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

zbraniecki / icu4x / 6815798908

09 Nov 2023 05:17PM UTC coverage: 72.607% (-2.4%) from 75.01%
6815798908

push

github

web-flow
Implement `Any/BufferProvider` for some smart pointers (#4255)

Allows storing them as a `Box<dyn Any/BufferProvider>` without using a
wrapper type that implements the trait.

44281 of 60987 relevant lines covered (72.61%)

201375.86 hits per line

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

78.38
/utils/tinystr/src/ule.rs
1
// This file is part of ICU4X. For terms of use, please see the file
×
2
// called LICENSE at the top level of the ICU4X source tree
3
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4

5
use crate::{TinyAsciiStr, UnvalidatedTinyAsciiStr};
6
use zerovec::maps::ZeroMapKV;
7
use zerovec::ule::*;
8
use zerovec::{ZeroSlice, ZeroVec};
9

10
// Safety (based on the safety checklist on the ULE trait):
11
//  1. TinyAsciiStr does not include any uninitialized or padding bytes.
12
//     (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
13
//  2. TinyAsciiStr is aligned to 1 byte.
14
//     (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
15
//  3. The impl of validate_byte_slice() returns an error if any byte is not valid.
16
//  4. The impl of validate_byte_slice() returns an error if there are extra bytes.
17
//  5. The other ULE methods use the default impl.
18
//  6. TinyAsciiStr byte equality is semantic equality
19
unsafe impl<const N: usize> ULE for TinyAsciiStr<N> {
20
    #[inline]
21
    fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
38✔
22
        if bytes.len() % N != 0 {
38✔
23
            return Err(ZeroVecError::length::<Self>(bytes.len()));
×
24
        }
25
        // Validate the bytes
26
        for chunk in bytes.chunks_exact(N) {
167✔
27
            let _ = TinyAsciiStr::<N>::from_bytes_inner(chunk, 0, N, true)
129✔
28
                .map_err(|_| ZeroVecError::parse::<Self>())?;
×
29
        }
30
        Ok(())
38✔
31
    }
38✔
32
}
33

34
impl<const N: usize> AsULE for TinyAsciiStr<N> {
35
    type ULE = Self;
36

37
    #[inline]
38
    fn to_unaligned(self) -> Self::ULE {
146✔
39
        self
146✔
40
    }
146✔
41

42
    #[inline]
43
    fn from_unaligned(unaligned: Self::ULE) -> Self {
857✔
44
        unaligned
857✔
45
    }
857✔
46
}
47

48
impl<'a, const N: usize> ZeroMapKV<'a> for TinyAsciiStr<N> {
49
    type Container = ZeroVec<'a, TinyAsciiStr<N>>;
50
    type Slice = ZeroSlice<TinyAsciiStr<N>>;
51
    type GetType = TinyAsciiStr<N>;
52
    type OwnedType = TinyAsciiStr<N>;
53
}
54

55
// Safety (based on the safety checklist on the ULE trait):
56
//  1. UnvalidatedTinyAsciiStr does not include any uninitialized or padding bytes.
57
//     (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
58
//  2. UnvalidatedTinyAsciiStr is aligned to 1 byte.
59
//     (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
60
//  3. The impl of validate_byte_slice() returns an error if any byte is not valid.
61
//  4. The impl of validate_byte_slice() returns an error if there are extra bytes.
62
//  5. The other ULE methods use the default impl.
63
//  6. UnvalidatedTinyAsciiStr byte equality is semantic equality
64
unsafe impl<const N: usize> ULE for UnvalidatedTinyAsciiStr<N> {
65
    #[inline]
66
    fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
×
67
        if bytes.len() % N != 0 {
×
68
            return Err(ZeroVecError::length::<Self>(bytes.len()));
×
69
        }
70
        Ok(())
×
71
    }
×
72
}
73

74
impl<const N: usize> AsULE for UnvalidatedTinyAsciiStr<N> {
75
    type ULE = Self;
76

77
    #[inline]
78
    fn to_unaligned(self) -> Self::ULE {
32,520✔
79
        self
32,520✔
80
    }
32,520✔
81

82
    #[inline]
83
    fn from_unaligned(unaligned: Self::ULE) -> Self {
209,499✔
84
        unaligned
209,499✔
85
    }
209,499✔
86
}
87

88
impl<'a, const N: usize> ZeroMapKV<'a> for UnvalidatedTinyAsciiStr<N> {
89
    type Container = ZeroVec<'a, UnvalidatedTinyAsciiStr<N>>;
90
    type Slice = ZeroSlice<UnvalidatedTinyAsciiStr<N>>;
91
    type GetType = UnvalidatedTinyAsciiStr<N>;
92
    type OwnedType = UnvalidatedTinyAsciiStr<N>;
93
}
94

95
#[cfg(test)]
96
mod test {
97
    use crate::*;
98
    use zerovec::*;
99

100
    #[test]
101
    fn test_zerovec() {
2✔
102
        let mut vec = ZeroVec::<TinyAsciiStr<7>>::new();
1✔
103

104
        vec.with_mut(|v| v.push("foobar".parse().unwrap()));
2✔
105
        vec.with_mut(|v| v.push("baz".parse().unwrap()));
2✔
106
        vec.with_mut(|v| v.push("quux".parse().unwrap()));
2✔
107

108
        let bytes = vec.as_bytes();
1✔
109

110
        let vec: ZeroVec<TinyAsciiStr<7>> = ZeroVec::parse_byte_slice(bytes).unwrap();
1✔
111

112
        assert_eq!(&*vec.get(0).unwrap(), "foobar");
1✔
113
        assert_eq!(&*vec.get(1).unwrap(), "baz");
1✔
114
        assert_eq!(&*vec.get(2).unwrap(), "quux");
1✔
115
    }
2✔
116
}
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