Skip to content

Commit c3a0885

Browse files
committed
make new unsafe and add safety docs
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent a490e78 commit c3a0885

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
//!
3232
//! ### Destructor safety
3333
//!
34-
//! TODO
34+
//! An `OwnedBuf` takes a destructor as a function pointer and calls it from the `OwnedBuf`'s
35+
//! destructor. The function pointer is marked as `unsafe` and the safety invariant is that the
36+
//! `OwnedBuf` was created from the collection type expected by the destructor function. This
37+
//! invariant must be ensured when the `OwnedBuf` is created, thus `OwnedBuf::new` is `unsafe`.
3538
//!
3639
//! ## Conversion from user types
3740
//!
@@ -111,9 +114,9 @@ impl OwnedBuf {
111114
///
112115
/// # Safety
113116
///
114-
/// See module docs for safety requirements on the destructor function.
117+
/// See module docs for safety requirements.
115118
#[inline]
116-
pub fn new(
119+
pub unsafe fn new(
117120
data: *mut MaybeUninit<u8>,
118121
dtor: unsafe fn(&mut OwnedBuf),
119122
capacity: usize,
@@ -268,7 +271,7 @@ impl Drop for OwnedBuf {
268271

269272
unsafe fn drop_vec(buf: &mut OwnedBuf) {
270273
let (data, _, filled, _, capacity) = unsafe { ptr::read(buf) }.into_raw_parts();
271-
let _vec = unsafe { Vec::from_raw_parts(data, filled, capacity) };
274+
let _vec = Vec::from_raw_parts(data, filled, capacity);
272275
}
273276

274277
impl From<Vec<MaybeUninit<u8>>> for OwnedBuf {

0 commit comments

Comments
 (0)