Skip to content

Commit c6a8158

Browse files
committed
docs: safety comments
1 parent 8947fed commit c6a8158

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ pin-project-lite = "0.2"
1919
rust_2018_idioms = "deny"
2020
missing_debug_implementations = { level = "deny", priority = -1 }
2121
# missing_docs = { level = "deny", priority = -1 }
22+
23+
[workspace.lints.clippy]
24+
undocumented_unsafe_blocks = "deny"

crates/compression-codecs/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@ liblzma = { version = "0.4.5", optional = true }
5656
memchr = { version = "2", optional = true }
5757
zstd-safe = { version = "7", optional = true, default-features = false }
5858

59+
[dev-dependencies]
60+
static_assertions = "1"
61+
5962
[lints]
6063
workspace = true

crates/compression-codecs/src/zstd/mod.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,6 @@ trait WriteBufExt {
3535

3636
impl WriteBufExt for WriteBuffer<'_> {
3737
fn get_out_buf(&mut self) -> OutBuffer<'_, WriteBufferWrapper<'_>> {
38-
{
39-
use std::mem::{align_of, size_of};
40-
assert_eq!(
41-
size_of::<WriteBuffer<'static>>(),
42-
size_of::<WriteBufferWrapper<'static>>()
43-
);
44-
assert_eq!(
45-
align_of::<WriteBuffer<'static>>(),
46-
align_of::<WriteBufferWrapper<'static>>()
47-
);
48-
}
49-
5038
// Pass written_len to avoid overwriting existing data in buffer.
5139
let written_len = self.written_len();
5240
OutBuffer::around_pos(
@@ -95,3 +83,11 @@ impl<C: Operation> OperationExt for Unshared<C> {
9583
Ok(self.get_mut().finish(&mut output.get_out_buf(), true)? == 0)
9684
}
9785
}
86+
87+
#[cfg(test)]
88+
mod tests {
89+
use super::*;
90+
91+
static_assertions::assert_eq_size!(WriteBuffer<'static>, WriteBufferWrapper<'static>);
92+
static_assertions::assert_eq_align!(WriteBuffer<'static>, WriteBufferWrapper<'static>);
93+
}

crates/compression-core/src/util.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::cmp;
12
use core::mem::MaybeUninit;
23

34
pub const fn _assert_send<T: Send>() {}
@@ -18,7 +19,7 @@ impl<B: AsRef<[u8]>> PartialBuffer<B> {
1819
&self.buffer.as_ref()[..self.index]
1920
}
2021

21-
/// Convenient method for `.writen().len()`
22+
/// Convenience method for `.written().len()`.
2223
pub fn written_len(&self) -> usize {
2324
self.index
2425
}
@@ -75,14 +76,11 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> From<B> for PartialBuffer<B> {
7576

7677
/// Write buffer for compression-codecs.
7778
///
78-
/// Currently it only supports initialized buffer, but will support uninitialized
79-
/// buffer soon.
80-
///
8179
/// # Layout
8280
///
8381
/// ```text
84-
/// | buffer |
85-
/// | written and initialized | unwritten but initialized | unwritten and uninitialized
82+
/// | buffer |
83+
/// | written and initialized | unwritten but initialized | unwritten and uninitialized |
8684
/// ```
8785
#[derive(Debug)]
8886
pub struct WriteBuffer<'a> {
@@ -110,6 +108,7 @@ impl<'a> WriteBuffer<'a> {
110108
}
111109
}
112110

111+
/// Returns entire buffer capacity, including space which is not yet initialized.
113112
pub fn capacity(&self) -> usize {
114113
self.buffer.len()
115114
}
@@ -118,17 +117,23 @@ impl<'a> WriteBuffer<'a> {
118117
self.buffer.as_mut_ptr() as *mut _
119118
}
120119

120+
/// Returns size of buffer's initialized portion.
121+
///
122+
/// This will always be at least [`written_len`](Self::written_len).
121123
pub fn initialized_len(&self) -> usize {
122124
self.initialized
123125
}
124126

125127
pub fn written(&self) -> &[u8] {
126128
debug_assert!(self.index <= self.initialized);
127129

130+
// SAFETY: slice up to `index` is always initialized
128131
unsafe { &*(&self.buffer[..self.index] as *const _ as *const [u8]) }
129132
}
130133

131-
/// Convenient method for `.writen().len()`
134+
/// Returns size of buffer's written portion.
135+
///
136+
/// This will always be at most [`initialized_len`](Self::initialized_len).
132137
pub fn written_len(&self) -> usize {
133138
self.index
134139
}
@@ -148,6 +153,7 @@ impl<'a> WriteBuffer<'a> {
148153
});
149154
self.initialized = self.buffer.len();
150155

156+
// SAFETY: slice up to `index` is always initialized
151157
unsafe { &mut *(&mut self.buffer[self.index..] as *mut _ as *mut [u8]) }
152158
}
153159

@@ -202,7 +208,7 @@ impl<'a> WriteBuffer<'a> {
202208
debug_assert!(self.index + n <= self.buffer.len());
203209

204210
self.index += n;
205-
self.initialized = self.initialized.max(self.index);
211+
self.initialized = cmp::max(self.initialized, self.index);
206212
}
207213

208214
/// Convenient function combining [`WriteBuffer::assume_init`] and [`WriteBuffer::advance`],
@@ -215,15 +221,15 @@ impl<'a> WriteBuffer<'a> {
215221
debug_assert!(n <= self.buffer.len());
216222

217223
self.index = n;
218-
self.initialized = self.initialized.max(n);
224+
self.initialized = cmp::max(self.initialized, n);
219225
}
220226

221227
pub fn copy_unwritten_from<C: AsRef<[u8]>>(&mut self, other: &mut PartialBuffer<C>) -> usize {
222228
fn inner(this: &mut WriteBuffer<'_>, input: &[u8]) -> usize {
223229
// Safety: We will never ever write uninitialized bytes into it
224230
let out = unsafe { this.unwritten_mut() };
225231

226-
let len = out.len().min(input.len());
232+
let len = cmp::min(out.len(), input.len());
227233

228234
out[..len]
229235
.iter_mut()

0 commit comments

Comments
 (0)