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

vortex-data / vortex / 16935267080

13 Aug 2025 11:00AM UTC coverage: 24.312% (-63.3%) from 87.658%
16935267080

Pull #4226

github

web-flow
Merge 81b48c7fb into baa6ea202
Pull Request #4226: Support converting TimestampTZ to and from duckdb

0 of 2 new or added lines in 1 file covered. (0.0%)

20666 existing lines in 469 files now uncovered.

8726 of 35892 relevant lines covered (24.31%)

147.74 hits per line

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

62.5
/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 {
6,788✔
24
        assert!(align > 0, "Alignment must be greater than 0");
6,788✔
25
        assert!(align <= u16::MAX as usize, "Alignment must fit into u16");
6,788✔
26
        assert!(align.is_power_of_two(), "Alignment must be a power of 2");
6,788✔
27
        Self(align)
6,788✔
28
    }
6,788✔
29

30
    /// Create a new 1-byte alignment.
31
    pub const fn none() -> Self {
62✔
32
        Self::new(1)
62✔
33
    }
62✔
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 {
6,654✔
48
        Self::new(align_of::<T>())
6,654✔
49
    }
6,654✔
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 {
3,460✔
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()
3,460✔
68
    }
3,460✔
69

70
    /// Returns the log2 of the alignment.
71
    pub fn exponent(&self) -> u8 {
48✔
72
        u8::try_from(self.0.trailing_zeros())
48✔
73
            .vortex_expect("alignment fits into u16, so exponent fits in u7")
48✔
74
    }
48✔
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 {
22✔
83
        Self::new(1 << exponent)
22✔
84
    }
22✔
85
}
86

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

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

96
    fn deref(&self) -> &Self::Target {
6,836✔
97
        &self.0
6,836✔
98
    }
6,836✔
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() {
132
        Alignment::new(0);
133
    }
134

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

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

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

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