Skip to content

Commit c36bbe8

Browse files
committed
Remove unsafe{}
1 parent cba077f commit c36bbe8

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

src/flatbuffers/unsafe_tools.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Unsafe utility functions for working with flatbuffers and other low-level operations.
22
33
use crate::filters::flatbuffer_generated::fb;
4-
use std::mem::MaybeUninit;
54

65
// Minimum alignment for the beginning of the flatbuffer data.
76
const MIN_ALIGNMENT: usize = 8;
@@ -99,37 +98,27 @@ impl VerifiedFlatbufferMemory {
9998
}
10099
}
101100

102-
/// A simple stack-allocated vector.
103-
/// It is used to avoid allocations when the vector is small.
101+
/// A stack-allocated vector that uses [T; MAX_SIZE] with Default initialization.
102+
/// All elements are initialized to T::default(), and we track the logical size separately.
103+
/// Note: a future impl can switch to using MaybeUninit with unsafe code for better efficiency.
104104
pub struct StackVector<T, const MAX_SIZE: usize> {
105-
data: [MaybeUninit<T>; MAX_SIZE],
105+
data: [T; MAX_SIZE],
106106
size: usize,
107107
}
108108

109-
impl<T, const MAX_SIZE: usize> Default for StackVector<T, MAX_SIZE>
110-
where
111-
T: Default + Copy,
112-
{
109+
impl<T: Default + Copy, const MAX_SIZE: usize> Default for StackVector<T, MAX_SIZE> {
113110
fn default() -> Self {
114111
Self {
115-
data: [MaybeUninit::uninit(); MAX_SIZE],
112+
data: [T::default(); MAX_SIZE],
116113
size: 0,
117114
}
118115
}
119116
}
120117

121-
impl<T, const MAX_SIZE: usize> Drop for StackVector<T, MAX_SIZE> {
122-
fn drop(&mut self) {
123-
for i in 0..self.size {
124-
unsafe { self.data[i].assume_init_drop() };
125-
}
126-
}
127-
}
128-
129-
impl<T, const MAX_SIZE: usize> StackVector<T, MAX_SIZE> {
118+
impl<T: Default, const MAX_SIZE: usize> StackVector<T, MAX_SIZE> {
130119
pub fn push(&mut self, value: T) -> bool {
131120
if self.size < MAX_SIZE {
132-
self.data[self.size] = MaybeUninit::new(value);
121+
self.data[self.size] = value;
133122
self.size += 1;
134123
true
135124
} else {
@@ -142,21 +131,19 @@ impl<T, const MAX_SIZE: usize> StackVector<T, MAX_SIZE> {
142131
}
143132

144133
pub fn clear(&mut self) {
145-
for i in 0..self.size {
146-
unsafe { self.data[i].assume_init_drop() };
147-
}
148134
self.size = 0;
149135
}
150136

151137
pub fn as_slice(&self) -> &[T] {
152-
unsafe { std::slice::from_raw_parts(self.data.as_ptr() as *const T, self.size) }
138+
&self.data[..self.size]
153139
}
154140

155-
pub fn into_vec(self) -> Vec<T> {
141+
pub fn into_vec(mut self) -> Vec<T> {
156142
let mut v = Vec::with_capacity(self.size);
157143
for i in 0..self.size {
158-
v.push(unsafe { self.data[i].assume_init_read() });
144+
v.push(std::mem::take(&mut self.data[i]));
159145
}
146+
self.size = 0;
160147
v
161148
}
162149
}

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn is_allowed_filter(ch: char) -> bool {
2929
ch.is_alphanumeric() || ch == '%'
3030
}
3131

32-
pub type TokensBuffer = StackVector<Hash, 200>;
32+
pub type TokensBuffer = StackVector<Hash, 256>;
3333

3434
fn fast_tokenizer_no_regex(
3535
pattern: &str,

0 commit comments

Comments
 (0)