Skip to content

Commit 925cd56

Browse files
committed
doc: Improve API doc and comments and remove unneeded doc and comments
1 parent 558bbea commit 925cd56

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ repository = "https://github.com/jhg/opaque-pointer-rs/"
1313
[features]
1414
default = ["std"]
1515
std = []
16+
# If you does not use std then you need alloc feature
1617
alloc = []
18+
# Allow to use some functions for FFI C types
1719
c-types = []
20+
# Check pointers before to use it to panic if it is null

src/c.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
//! # C types FFI opaque pointers.
2-
//!
3-
//! Specific C types like C-like string pointers.
1+
//! # Specific C types like C-like string pointers.
42
53
#![cfg(all(feature = "std", feature = "c-types"))]
64

@@ -9,11 +7,11 @@ use std::ffi::CStr;
97

108
use super::panic_if_null;
119

12-
/// Reference to a C string.
10+
/// Convert a reference to a C string into a static reference to Rust `str`.
1311
///
1412
/// # Safety
1513
///
16-
/// The pointer must be a valid reference to that value with that type.
14+
/// The pointer must be a valid reference or behavior is undefined.
1715
///
1816
/// # Panics
1917
///

src/lib.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,27 @@ use std::boxed::Box;
2424
#[cfg(all(feature = "std", feature = "c-types"))]
2525
pub mod c;
2626

27-
/// Panic if a pointer is null.
2827
#[inline]
2928
fn panic_if_null<T>(pointer: *const T) {
3029
if pointer.is_null() {
3130
unreachable!("A null pointer was passed to the library, something is wrong in the C or C++ code");
3231
}
3332
}
3433

35-
/// Convert type to raw pointer.
34+
/// Convert type to raw pointer ready to be used as opaque pointer.
3635
#[cfg(any(feature = "alloc", feature = "std"))]
3736
#[inline]
3837
pub fn raw<T>(data: T) -> *mut T {
3938
return Box::into_raw(Box::new(data));
4039
}
4140

42-
/// Free pointer to type.
41+
/// Free memory of a previous type converted to raw pointer.
4342
///
4443
/// # Safety
4544
///
46-
/// Never call it twice. That could produce a HEAP error that produce a crash.
45+
/// The pointer must be a valid reference and never call it twice or behavior is undefined.
46+
///
47+
/// That could produce a HEAP error that produce a crash.
4748
#[cfg(any(feature = "alloc", feature = "std"))]
4849
#[inline]
4950
pub unsafe fn free<T>(pointer: *mut T) {
@@ -58,11 +59,13 @@ pub unsafe fn free<T>(pointer: *mut T) {
5859
// We let drop the boxed data.
5960
}
6061

61-
/// Own back from a raw pointer.
62+
/// Own back from a raw pointer to use Rust ownership as usually.
6263
///
6364
/// # Safety
6465
///
65-
/// Never call it twice. That could produce a HEAP error that produce a crash.
66+
/// The pointer must be a valid reference and never call it twice or behavior is undefined.
67+
///
68+
/// That could produce a HEAP error that produce a crash.
6669
#[cfg(any(feature = "alloc", feature = "std"))]
6770
#[inline]
6871
pub unsafe fn own_back<T>(pointer: *mut T) -> T {
@@ -72,23 +75,33 @@ pub unsafe fn own_back<T>(pointer: *mut T) -> T {
7275
return *boxed;
7376
}
7477

75-
/// Convert raw pointer to type to type reference.
78+
/// Convert raw pointer to type to type reference but without back to own it.
79+
///
80+
/// That's the main difference with `own_back<T>`, it does not back to use ownership
81+
/// and values will not be dropped.
7682
///
7783
/// # Safety
7884
///
79-
/// The pointer must be a valid reference to that value with that type.
85+
/// The pointer must be a valid reference and never call it twice or behavior is undefined.
86+
///
87+
/// That could produce a HEAP error that produce a crash.
8088
#[inline]
8189
pub unsafe fn object<'a, T>(pointer: *const T) -> &'a T {
8290
panic_if_null(pointer);
8391
// CAUTION: this is unsafe
8492
return &*pointer;
8593
}
8694

87-
/// Convert raw pointer to type into type mutable reference.
95+
/// Convert raw pointer to type into type mutable reference but without back to own it.
96+
///
97+
/// That's the main difference with `own_back<T>`, it does not back to use ownership
98+
/// and values will not be dropped.
8899
///
89100
/// # Safety
90101
///
91-
/// The pointer must be a valid reference to that value with that type.
102+
/// The pointer must be a valid reference and never call it twice or behavior is undefined.
103+
///
104+
/// That could produce a HEAP error that produce a crash.
92105
#[inline]
93106
pub unsafe fn mut_object<'a, T>(pointer: *mut T) -> &'a mut T {
94107
panic_if_null(pointer);

0 commit comments

Comments
 (0)