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

zbraniecki / icu4x / 11904027177

19 Nov 2024 12:33AM UTC coverage: 75.477% (+0.3%) from 75.174%
11904027177

push

github

web-flow
Move DateTimePattern into pattern module (#5834)

#1317

Also removes `NeoNeverMarker` and fixes #5689

258 of 319 new or added lines in 6 files covered. (80.88%)

6967 existing lines in 278 files now uncovered.

54522 of 72237 relevant lines covered (75.48%)

655305.49 hits per line

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

91.67
/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_bytes() returns an error if any byte is not valid.
16
//  4. The impl of validate_bytes() 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_bytes(bytes: &[u8]) -> Result<(), UleError> {
13,001✔
22
        if bytes.len() % N != 0 {
13,001✔
23
            return Err(UleError::length::<Self>(bytes.len()));
×
24
        }
25
        // Validate the bytes
26
        for chunk in bytes.chunks_exact(N) {
26,238✔
27
            let _ = TinyAsciiStr::<N>::try_from_utf8_inner(chunk, true)
13,237✔
28
                .map_err(|_| UleError::parse::<Self>())?;
×
29
        }
30
        Ok(())
13,001✔
31
    }
13,001✔
32
}
33

34
impl<const N: usize> NicheBytes<N> for TinyAsciiStr<N> {
35
    // AsciiByte is 0..128
36
    const NICHE_BIT_PATTERN: [u8; N] = [255; N];
37
}
38

39
impl<const N: usize> AsULE for TinyAsciiStr<N> {
40
    type ULE = Self;
41

42
    #[inline]
43
    fn to_unaligned(self) -> Self::ULE {
1,010✔
44
        self
1,010✔
45
    }
1,010✔
46

47
    #[inline]
48
    fn from_unaligned(unaligned: Self::ULE) -> Self {
68,579✔
49
        unaligned
68,579✔
50
    }
68,579✔
51
}
52

53
impl<'a, const N: usize> ZeroMapKV<'a> for TinyAsciiStr<N> {
54
    type Container = ZeroVec<'a, TinyAsciiStr<N>>;
55
    type Slice = ZeroSlice<TinyAsciiStr<N>>;
56
    type GetType = TinyAsciiStr<N>;
57
    type OwnedType = TinyAsciiStr<N>;
58
}
59

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

79
impl<const N: usize> AsULE for UnvalidatedTinyAsciiStr<N> {
80
    type ULE = Self;
81

82
    #[inline]
83
    fn to_unaligned(self) -> Self::ULE {
50,045✔
84
        self
50,045✔
85
    }
50,045✔
86

87
    #[inline]
88
    fn from_unaligned(unaligned: Self::ULE) -> Self {
900,149✔
89
        unaligned
900,149✔
90
    }
900,149✔
91
}
92

93
impl<'a, const N: usize> ZeroMapKV<'a> for UnvalidatedTinyAsciiStr<N> {
94
    type Container = ZeroVec<'a, UnvalidatedTinyAsciiStr<N>>;
95
    type Slice = ZeroSlice<UnvalidatedTinyAsciiStr<N>>;
96
    type GetType = UnvalidatedTinyAsciiStr<N>;
97
    type OwnedType = UnvalidatedTinyAsciiStr<N>;
98
}
99

100
#[cfg(test)]
101
mod test {
102
    use crate::*;
103
    use zerovec::*;
104

105
    #[test]
106
    fn test_zerovec() {
2✔
107
        let mut vec = ZeroVec::<TinyAsciiStr<7>>::new();
1✔
108

109
        vec.with_mut(|v| v.push("foobar".parse().unwrap()));
2✔
110
        vec.with_mut(|v| v.push("baz".parse().unwrap()));
2✔
111
        vec.with_mut(|v| v.push("quux".parse().unwrap()));
2✔
112

113
        let bytes = vec.as_bytes();
1✔
114

115
        let vec: ZeroVec<TinyAsciiStr<7>> = ZeroVec::parse_bytes(bytes).unwrap();
1✔
116

117
        assert_eq!(&*vec.get(0).unwrap(), "foobar");
1✔
118
        assert_eq!(&*vec.get(1).unwrap(), "baz");
1✔
119
        assert_eq!(&*vec.get(2).unwrap(), "quux");
1✔
120
    }
2✔
121
}
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