Skip to content

Commit c74bd90

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

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
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-core/src/util.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<B: AsRef<[u8]>> PartialBuffer<B> {
1818
&self.buffer.as_ref()[..self.index]
1919
}
2020

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

7676
/// Write buffer for compression-codecs.
7777
///
78-
/// Currently it only supports initialized buffer, but will support uninitialized
79-
/// buffer soon.
80-
///
8178
/// # Layout
8279
///
8380
/// ```text
84-
/// | buffer |
85-
/// | written and initialized | unwritten but initialized | unwritten and uninitialized
81+
/// | buffer |
82+
/// | written and initialized | unwritten but initialized | unwritten and uninitialized |
8683
/// ```
8784
#[derive(Debug)]
8885
pub struct WriteBuffer<'a> {
@@ -110,6 +107,7 @@ impl<'a> WriteBuffer<'a> {
110107
}
111108
}
112109

110+
/// Returns entire buffer capacity, including space which is not yet initialized.
113111
pub fn capacity(&self) -> usize {
114112
self.buffer.len()
115113
}
@@ -118,17 +116,23 @@ impl<'a> WriteBuffer<'a> {
118116
self.buffer.as_mut_ptr() as *mut _
119117
}
120118

119+
/// Returns size of buffer's initialized portion.
120+
///
121+
/// This will always be at least [`written_len`](Self::written_len).
121122
pub fn initialized_len(&self) -> usize {
122123
self.initialized
123124
}
124125

125126
pub fn written(&self) -> &[u8] {
126127
debug_assert!(self.index <= self.initialized);
127128

129+
// SAFETY: slice up to `index` is always initialized
128130
unsafe { &*(&self.buffer[..self.index] as *const _ as *const [u8]) }
129131
}
130132

131-
/// Convenient method for `.writen().len()`
133+
/// Returns size of buffer's written portion.
134+
///
135+
/// This will always be at most [`initialized_len`](Self::initialized_len).
132136
pub fn written_len(&self) -> usize {
133137
self.index
134138
}
@@ -148,6 +152,7 @@ impl<'a> WriteBuffer<'a> {
148152
});
149153
self.initialized = self.buffer.len();
150154

155+
// SAFETY: slice up to `index` is always initialized
151156
unsafe { &mut *(&mut self.buffer[self.index..] as *mut _ as *mut [u8]) }
152157
}
153158

0 commit comments

Comments
 (0)