nlib 0.1.3

Nate's library. Various things or macro patterns I use to aid in more succint Rust programming.
Documentation
// Copyright 2025 Nathan Sizemore <nathanrsizemore@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.

use std::{ffi::CString, ptr};

/// Return a malloc’ed C string (caller must free with `from_raw`),
/// or `NULL` if `None`.
pub fn into_cstring_ptr<S: AsRef<str>>(value: Option<S>) -> *mut libc::c_char {
    match value {
        Some(s) => CString::new(s.as_ref())
            .map(|c| c.into_raw())
            .unwrap_or_else(|_| ptr::null_mut()),
        None => ptr::null_mut(),
    }
}

/// Reclaim (free) a heap-allocated C string previously created with
/// `CString::into_raw`, dropping it at the end of this function.
///
/// # Safety
///
/// This function **assumes** `p` was returned by `CString::into_raw` (or an
/// equivalent allocator-compatible API) and has not already been freed.
/// Passing any other pointer (e.g., to static storage, stack memory, or a
/// string owned by foreign code) is **undefined behavior**.
pub fn reclaim_cstring(p: *const libc::c_char) {
    if p.is_null() {
        return;
    }

    let mut_p = p.cast_mut();
    let _reclaim = unsafe { CString::from_raw(mut_p) };
}