-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathmacros.rs
More file actions
110 lines (107 loc) · 4.36 KB
/
macros.rs
File metadata and controls
110 lines (107 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
//! Helper macros for working with the different variants of [`PrimitiveVector`] and
//! [`PrimitiveVectorMut`].
//!
//! [`PrimitiveVector`]: crate::PrimitiveVector
//! [`PrimitiveVectorMut`]: crate::PrimitiveVectorMut
/// Matches on all primitive type variants of [`PrimitiveVector`] and executes the same code for
/// each variant branch.
///
/// This macro eliminates repetitive match statements when implementing operations that need to work
/// uniformly across all primitive type variants (`U8`, `U16`, `U32`, `U64`, `I8`, `I16`, `I32`,
/// `I64`, `F16`, `F32`, `F64`).
///
/// # Examples
///
/// ```
/// use vortex_vector::{PrimitiveVector, PVectorMut, VectorOps, VectorMutOps, match_each_pvector};
///
/// fn get_primitive_len(vector: &PrimitiveVector) -> usize {
/// match_each_pvector!(vector, |v| { v.len() })
/// }
///
/// // Works with `I32` primitive vectors.
/// let i32_vec: PrimitiveVector = PVectorMut::<i32>::from_iter([1, 2, 3].map(Some))
/// .freeze()
/// .into();
/// assert_eq!(get_primitive_len(&i32_vec), 3);
///
/// // Works with `F64` primitive vectors.
/// let f64_vec: PrimitiveVector = PVectorMut::<f64>::from_iter([1.0, 2.5].map(Some))
/// .freeze()
/// .into();
/// assert_eq!(get_primitive_len(&f64_vec), 2);
/// ```
///
/// Note: The `len` method is already provided by the [`VectorOps`] trait implementation.
///
/// [`PrimitiveVector`]: crate::PrimitiveVector
/// [`VectorOps`]: crate::VectorOps
#[macro_export]
macro_rules! match_each_pvector {
($self:expr, | $vec:ident | $body:block) => {{
match $self {
$crate::PrimitiveVector::U8($vec) => $body,
$crate::PrimitiveVector::U16($vec) => $body,
$crate::PrimitiveVector::U32($vec) => $body,
$crate::PrimitiveVector::U64($vec) => $body,
$crate::PrimitiveVector::I8($vec) => $body,
$crate::PrimitiveVector::I16($vec) => $body,
$crate::PrimitiveVector::I32($vec) => $body,
$crate::PrimitiveVector::I64($vec) => $body,
$crate::PrimitiveVector::F16($vec) => $body,
$crate::PrimitiveVector::F32($vec) => $body,
$crate::PrimitiveVector::F64($vec) => $body,
}
}};
}
/// Matches on all primitive type variants of [`PrimitiveVectorMut`] and executes the same code
/// for each variant branch.
///
/// This macro eliminates repetitive match statements when implementing mutable operations that need
/// to work uniformly across all primitive type variants (`U8`, `U16`, `U32`, `U64`, `I8`, `I16`,
/// `I32`, `I64`, `F16`, `F32`, `F64`).
///
/// # Examples
///
/// ```
/// use vortex_vector::{PrimitiveVectorMut, PVectorMut, VectorMutOps, match_each_pvector_mut};
///
/// fn reserve_primitive_space(vector: &mut PrimitiveVectorMut, additional: usize) {
/// match_each_pvector_mut!(vector, |v| { v.reserve(additional) })
/// }
///
/// // Works with `U8` mutable primitive vectors.
/// let mut u8_vec: PrimitiveVectorMut = PVectorMut::<u8>::from_iter([1, 2].map(Some)).into();
/// reserve_primitive_space(&mut u8_vec, 10);
/// assert!(u8_vec.capacity() >= 12);
///
/// // Works with `I64` mutable primitive vectors.
/// let mut i64_vec: PrimitiveVectorMut = PVectorMut::<i64>::from_iter([100].map(Some)).into();
/// reserve_primitive_space(&mut i64_vec, 5);
/// assert!(i64_vec.capacity() >= 6);
/// ```
///
/// Note: The `reserve` method is already provided by the [`VectorMutOps`] trait implementation.
///
/// [`PrimitiveVectorMut`]: crate::PrimitiveVectorMut
/// [`VectorMutOps`]: crate::VectorMutOps
#[macro_export]
macro_rules! match_each_pvector_mut {
($self:expr, | $vec:ident | $body:block) => {{
match $self {
$crate::PrimitiveVectorMut::U8($vec) => $body,
$crate::PrimitiveVectorMut::U16($vec) => $body,
$crate::PrimitiveVectorMut::U32($vec) => $body,
$crate::PrimitiveVectorMut::U64($vec) => $body,
$crate::PrimitiveVectorMut::I8($vec) => $body,
$crate::PrimitiveVectorMut::I16($vec) => $body,
$crate::PrimitiveVectorMut::I32($vec) => $body,
$crate::PrimitiveVectorMut::I64($vec) => $body,
$crate::PrimitiveVectorMut::F16($vec) => $body,
$crate::PrimitiveVectorMut::F32($vec) => $body,
$crate::PrimitiveVectorMut::F64($vec) => $body,
}
}};
}