1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
use std::default::Default;
use std::marker::PhantomData;
/// Boxed is used to model values that are passed by reference and where their memory allocation is
/// managed entirely by Rust. These are represented in the C API by a pointer, with "new" and
/// "free" functions handling creation and destruction.
///
/// The value may be opaque to C, so that it may not access fields in the struct directly, in which
/// case `RType` can be any Rust type. Otherwise, if a C structure is provided, you must use
/// `#[repr(C)]` to ensure that C and Rust lay out the struct identically.
///
/// # Example
///
/// Define your C and Rust types, then a type alias parameterizing Boxed:
///
/// ```
/// # use ffizz_passby::Boxed;
/// struct System {
/// // ...
/// }
/// type BoxedSystem = Boxed<System>;
/// ```
///
/// Then call static methods on that type alias.
#[non_exhaustive]
pub struct Boxed<RType: Sized> {
_phantom: PhantomData<RType>,
}
impl<RType: Sized> Boxed<RType> {
/// Take a value from C as an argument, taking ownership of the value it points to.
///
/// Be careful that the C API documents that the passed pointer cannot be used after this
/// function is called.
///
/// If you would like to borrow the value, but leave ownership with the calling C code, use
/// [`Boxed::with_ref`] or its variants.
///
/// This function is most common in "free" functions, but can also be used in contexts where it
/// is ergonomic for the called function to consume the value. For example, a database
/// connections's `execute` method might reasonably consume a query argument.
///
/// ```c
/// db_query_t q = db_query_new();
/// db_query_set_filter(q, "x = 10");
/// db_query_add_column(q, "y");
/// db_result_t res = db_execute(db, q);
/// ```
///
/// Here it's natural to assume (but should also be documented) that the `db_execute`
/// function takes ownership of the query.
///
/// # Safety
///
/// * `arg` must not be NULL (see [`Boxed::take`] for a version allowing NULL).
/// * `arg` must be a value returned from `Box::into_raw` (via [`Boxed::return_val`] or [`Boxed::to_out_param`] or a variant).
/// * `arg` becomes invalid and must not be used after this call.
pub unsafe fn take_nonnull(arg: *mut RType) -> RType {
debug_assert!(!arg.is_null());
// SAFETY: see docstring
unsafe { *(Box::from_raw(arg)) }
}
/// Call the contained function with a shared reference to the value.
///
/// # Safety
///
/// * `arg` must not be NULL (see [`Boxed::with_ref`] for a version allowing NULL).
/// * No other thread may mutate the value pointed to by `arg` until this function returns.
/// * Ownership of the value remains with the caller.
pub unsafe fn with_ref_nonnull<T, F: FnOnce(&RType) -> T>(arg: *const RType, f: F) -> T {
if arg.is_null() {
panic!("NULL value not allowed");
}
// SAFETY:
// - pointer came from Box::into_raw, so has proper size and alignment
f(unsafe { &*(arg as *const RType) })
}
/// Call the contained function with an exclusive reference to the value.
///
/// # Safety
///
/// * `arg` must not be NULL (see [`Boxed::with_ref_mut`] for a version allowing null)
/// * No other thread may _access_ the value pointed to by `arg` until this function returns.
/// * Ownership of the value remains with the caller.
pub unsafe fn with_ref_mut_nonnull<T, F: FnOnce(&mut RType) -> T>(arg: *mut RType, f: F) -> T {
if arg.is_null() {
panic!("NULL value not allowed");
}
// SAFETY:
// - pointer came from Box::into_raw, so has proper size and alignment
f(unsafe { &mut *arg })
}
/// Return a value to C, boxing the value and transferring ownership.
///
/// This method is most often used in constructors, to return the built value.
///
/// # Safety
///
/// * The caller must ensure that the value is eventually freed.
pub unsafe fn return_val(rval: RType) -> *mut RType {
// SAFETY: return_val_boxed and return_val have the same safety requirements.
unsafe { Self::return_val_boxed(Box::new(rval)) }
}
/// Return a boxed value to C, transferring ownership.
///
/// This is an alternative to [`Boxed::return_val`] for use when the value is already boxed.
///
/// # Safety
///
/// * The caller must ensure that the value is eventually freed.
pub unsafe fn return_val_boxed(rval: Box<RType>) -> *mut RType {
Box::into_raw(rval)
}
/// Return a value to C, transferring ownership, via an "output parameter".
///
/// If the pointer is NULL, the value is dropped. Use [`Boxed::to_out_param_nonnull`] to panic
/// in this situation.
///
/// # Safety
///
/// * The caller must ensure that the value is eventually freed.
/// * If not NULL, `arg_out` must point to valid, properly aligned memory for a pointer value.
pub unsafe fn to_out_param(rval: RType, arg_out: *mut *mut RType) {
if !arg_out.is_null() {
// SAFETY: see docstring
unsafe { *arg_out = Self::return_val(rval) };
}
}
/// Return a value to C, transferring ownership, via an "output parameter".
///
/// If the pointer is NULL, this function will panic. Use [`Boxed::to_out_param`] to
/// drop the value in this situation.
///
/// # Safety
///
/// * The caller must ensure that the value is eventually freed.
/// * `arg_out` must not be NULL.
/// * `arg_out` must point to valid, properly aligned memory for a pointer value.
pub unsafe fn to_out_param_nonnull(rval: RType, arg_out: *mut *mut RType) {
if arg_out.is_null() {
panic!("out param pointer is NULL");
}
// SAFETY: see docstring
unsafe { *arg_out = Self::return_val(rval) };
}
}
impl<RType: Sized + Default> Boxed<RType> {
/// Take a value from C as an argument.
///
/// This function is similar to [`Boxed::take_nonnull`], but returns the default value of RType when
/// given NULL.
///
/// # Safety
///
/// * `arg` must be a value returned from `Box::into_raw` (via [`Boxed::return_val`] or [`Boxed::to_out_param`] or a variant).
/// * `arg` becomes invalid and must not be used after this call.
pub unsafe fn take(arg: *mut RType) -> RType {
debug_assert!(!arg.is_null());
// SAFETY: see docstring
unsafe { *(Box::from_raw(arg)) }
}
/// Call the contained function with a shared reference to the value.
///
/// If the given pointer is NULL, the contained function is called with a reference to RType's
/// default value, which is subsequently dropped.
///
/// # Safety
///
/// * No other thread may mutate the value pointed to by `arg` until this function returns.
/// * Ownership of the value remains with the caller.
pub unsafe fn with_ref<T, F: FnOnce(&RType) -> T>(arg: *const RType, f: F) -> T {
if arg.is_null() {
let nullval = RType::default();
return f(&nullval);
}
// SAFETY:
// - pointer is not NULL (just checked)
// - pointer came from Box::into_raw, so has proper size and alignment
f(unsafe { &*(arg as *const RType) })
}
/// Call the contained function with an exclusive reference to the value.
///
/// If the given pointer is NULL, the contained function is called with a reference to RType's
/// default value, which is subsequently dropped.
///
/// # Safety
///
/// * No other thread may _access_ the value pointed to by `arg` until this function returns.
/// * Ownership of the value remains with the caller.
pub unsafe fn with_ref_mut<T, F: FnOnce(&mut RType) -> T>(arg: *mut RType, f: F) -> T {
if arg.is_null() {
let mut nullval = RType::default();
return f(&mut nullval);
}
// SAFETY:
// - pointer is not NULL (just checked)
// - pointer came from Box::into_raw, so has proper size and alignment
f(unsafe { &mut *arg })
}
}
#[cfg(test)]
mod test {
use super::*;
use std::mem;
#[derive(Default)]
struct RType(u32, u64);
type BoxedTuple = Boxed<RType>;
#[test]
fn intialize_and_with_methods() {
unsafe {
let mut cptr = mem::MaybeUninit::<*mut RType>::uninit();
BoxedTuple::to_out_param(RType(10, 20), cptr.as_mut_ptr());
let cptr = cptr.assume_init();
BoxedTuple::with_ref_nonnull(cptr, |rref| {
assert_eq!(rref.0, 10);
assert_eq!(rref.1, 20);
});
BoxedTuple::with_ref_mut_nonnull(cptr, |rref| {
assert_eq!(rref.0, 10);
assert_eq!(rref.1, 20);
rref.0 = 30;
});
BoxedTuple::with_ref_mut(cptr, |rref| {
assert_eq!(rref.0, 30);
rref.0 += 1;
assert_eq!(rref.1, 20);
rref.1 += 1;
});
BoxedTuple::with_ref(cptr, |rref| {
assert_eq!(rref.0, 31);
assert_eq!(rref.1, 21);
});
let rval = BoxedTuple::take(cptr);
assert_eq!(rval.0, 31);
assert_eq!(rval.1, 21);
let mut cptr = mem::MaybeUninit::<*mut RType>::uninit();
BoxedTuple::to_out_param_nonnull(RType(100, 200), cptr.as_mut_ptr());
let cptr = cptr.assume_init();
let rval = BoxedTuple::take(cptr);
assert_eq!(rval.0, 100);
assert_eq!(rval.1, 200);
}
}
#[test]
fn with_null_ptrs() {
unsafe {
BoxedTuple::with_ref_mut(std::ptr::null_mut(), |rref| {
assert_eq!(rref.0, 0);
assert_eq!(rref.1, 0);
rref.1 += 1;
});
BoxedTuple::with_ref(std::ptr::null(), |rref| {
assert_eq!(rref.0, 0);
assert_eq!(rref.1, 0);
});
}
}
#[test]
#[should_panic]
fn with_ref_nonnull_null() {
unsafe {
BoxedTuple::with_ref_nonnull(std::ptr::null(), |_| {});
}
}
#[test]
#[should_panic]
fn with_ref_mut_nonnull_null() {
unsafe {
BoxedTuple::with_ref_mut_nonnull(std::ptr::null_mut(), |_| {});
}
}
#[test]
fn to_out_param_null() {
unsafe {
BoxedTuple::to_out_param(RType(10, 20), std::ptr::null_mut());
// nothing happens
}
}
#[test]
#[should_panic]
fn to_out_param_nonnull_null() {
unsafe {
BoxedTuple::to_out_param_nonnull(RType(10, 20), std::ptr::null_mut());
// nothing happens
}
}
#[test]
fn return_val_take() {
unsafe {
let cptr = BoxedTuple::return_val(RType(10, 20));
let rval = BoxedTuple::take(cptr);
assert_eq!(rval.0, 10);
assert_eq!(rval.1, 20);
}
}
#[test]
fn return_val_boxed_take_nonnull() {
unsafe {
let cptr = BoxedTuple::return_val_boxed(Box::new(RType(10, 20)));
let rval = BoxedTuple::take_nonnull(cptr);
assert_eq!(rval.0, 10);
assert_eq!(rval.1, 20);
}
}
#[test]
#[should_panic]
fn take_nnull() {
unsafe {
let rval = BoxedTuple::take(std::ptr::null_mut());
assert_eq!(rval.0, 0);
assert_eq!(rval.1, 0);
}
}
#[test]
#[should_panic]
fn take_nonnull_null() {
unsafe {
BoxedTuple::take_nonnull(std::ptr::null_mut());
}
}
}