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

vortex-data / vortex / 17075133033

19 Aug 2025 04:01PM UTC coverage: 87.949% (+0.09%) from 87.856%
17075133033

push

github

web-flow
feat: ArrayOperations infallible, eager validation + new_unchecked (#4177)

ArrayOperations currently return VortexResult<>, but they really should
just be infallible. A failed array op is generally indicative of
programmer or encoding error. There's really nothing interesting we can
do to handle an out-of-bounds slice() or scalar_at.

There's a lot that falls out of this, like fixing a bunch of tests,
tweaking our scalar value casting to return Option instead of Result,
etc.

---------

Signed-off-by: Andrew Duffy <andrew@a10y.dev>

1744 of 1985 new or added lines in 195 files covered. (87.86%)

36 existing lines in 27 files now uncovered.

56745 of 64520 relevant lines covered (87.95%)

624082.56 hits per line

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

80.0
/vortex-buffer/src/alignment.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use std::fmt::Display;
5
use std::ops::Deref;
6

7
use vortex_error::VortexExpect;
8

9
/// The alignment of a buffer.
10
///
11
/// This type is a wrapper around `usize` that ensures the alignment is a power of 2 and fits into
12
/// a `u16`.
13
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14
pub struct Alignment(usize);
15

16
impl Alignment {
17
    /// Create a new alignment.
18
    ///
19
    /// ## Panics
20
    ///
21
    /// Panics if `align` is not a power of 2, or is greater than `u16::MAX`.
22
    #[inline]
23
    pub const fn new(align: usize) -> Self {
23,023,344✔
24
        assert!(align > 0, "Alignment must be greater than 0");
23,023,344✔
25
        assert!(align <= u16::MAX as usize, "Alignment must fit into u16");
23,023,343✔
26
        assert!(align.is_power_of_two(), "Alignment must be a power of 2");
23,023,342✔
27
        Self(align)
23,023,341✔
28
    }
23,023,341✔
29

30
    /// Create a new 1-byte alignment.
31
    pub const fn none() -> Self {
78,926✔
32
        Self::new(1)
78,926✔
33
    }
78,926✔
34

35
    /// Create an alignment from the alignment of a type `T`.
36
    ///
37
    /// ## Example
38
    ///
39
    /// ```
40
    /// use vortex_buffer::Alignment;
41
    ///
42
    /// assert_eq!(Alignment::new(4), Alignment::of::<i32>());
43
    /// assert_eq!(Alignment::new(8), Alignment::of::<i64>());
44
    /// assert_eq!(Alignment::new(16), Alignment::of::<u128>());
45
    /// ```
46
    #[inline]
47
    pub const fn of<T>() -> Self {
22,379,099✔
48
        Self::new(align_of::<T>())
22,379,099✔
49
    }
22,379,099✔
50

51
    /// Check if this alignment is a "larger" than another alignment.
52
    ///
53
    /// ## Example
54
    ///
55
    /// ```
56
    /// use vortex_buffer::Alignment;
57
    ///
58
    /// let a = Alignment::new(4);
59
    /// let b = Alignment::new(2);
60
    /// assert!(a.is_aligned_to(b));
61
    /// assert!(!b.is_aligned_to(a));
62
    /// ```
63
    #[inline]
64
    pub fn is_aligned_to(&self, other: Alignment) -> bool {
14,739,005✔
65
        // Since we know alignments are powers of 2, we can compare them by checking if the number
66
        // of trailing zeros in the binary representation of the alignment is greater or equal.
67
        self.0.trailing_zeros() >= other.0.trailing_zeros()
14,739,005✔
68
    }
14,739,005✔
69

70
    /// Returns the log2 of the alignment.
71
    pub fn exponent(&self) -> u8 {
44,527✔
72
        u8::try_from(self.0.trailing_zeros())
44,527✔
73
            .vortex_expect("alignment fits into u16, so exponent fits in u7")
44,527✔
74
    }
44,527✔
75

76
    /// Create from the log2 exponent of the alignment.
77
    ///
78
    /// ## Panics
79
    ///
80
    /// Panics if `alignment` is not a power of 2, or is greater than `u16::MAX`.
81
    #[inline]
82
    pub const fn from_exponent(exponent: u8) -> Self {
24,293✔
83
        Self::new(1 << exponent)
24,293✔
84
    }
24,293✔
85
}
86

87
impl Display for Alignment {
88
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
882✔
89
        write!(f, "{}", self.0)
882✔
90
    }
882✔
91
}
92

93
impl Deref for Alignment {
94
    type Target = usize;
95

96
    fn deref(&self) -> &Self::Target {
28,502,381✔
97
        &self.0
28,502,381✔
98
    }
28,502,381✔
99
}
100

101
impl From<usize> for Alignment {
UNCOV
102
    fn from(value: usize) -> Self {
×
UNCOV
103
        Self::new(value)
×
UNCOV
104
    }
×
105
}
106

107
impl From<u16> for Alignment {
108
    fn from(value: u16) -> Self {
×
109
        Self::new(usize::from(value))
×
110
    }
×
111
}
112

113
impl From<Alignment> for usize {
114
    fn from(value: Alignment) -> Self {
×
115
        value.0
×
116
    }
×
117
}
118

119
impl From<Alignment> for u16 {
120
    fn from(value: Alignment) -> Self {
×
121
        u16::try_from(value.0).vortex_expect("Alignment must fit into u16")
×
122
    }
×
123
}
124

125
#[cfg(test)]
126
mod test {
127
    use super::*;
128

129
    #[test]
130
    #[should_panic]
131
    fn alignment_zero() {
1✔
132
        Alignment::new(0);
1✔
133
    }
1✔
134

135
    #[test]
136
    #[should_panic]
137
    fn alignment_overflow() {
1✔
138
        Alignment::new(u16::MAX as usize + 1);
1✔
139
    }
1✔
140

141
    #[test]
142
    #[should_panic]
143
    fn alignment_not_power_of_two() {
1✔
144
        Alignment::new(3);
1✔
145
    }
1✔
146

147
    #[test]
148
    fn alignment_exponent() {
1✔
149
        let alignment = Alignment::new(1024);
1✔
150
        assert_eq!(alignment.exponent(), 10);
1✔
151
        assert_eq!(Alignment::from_exponent(10), alignment);
1✔
152
    }
1✔
153

154
    #[test]
155
    fn is_aligned_to() {
1✔
156
        assert!(Alignment::new(1).is_aligned_to(Alignment::new(1)));
1✔
157
        assert!(Alignment::new(2).is_aligned_to(Alignment::new(1)));
1✔
158
        assert!(Alignment::new(4).is_aligned_to(Alignment::new(1)));
1✔
159
        assert!(!Alignment::new(1).is_aligned_to(Alignment::new(2)));
1✔
160
    }
1✔
161
}
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