mbox: malloc-based box

Travis (Linux and OS X) Build status AppVeyor (Windows) Build status Coverage Status crates.io MIT

This crate provides structures that wrap pointers returned from malloc as a Box, and automatically free them on drop. These types allow you to interact with pointers and null-terminated strings and arrays in a Rusty style.

Examples

```rust extern crate libc; extern crate mbox;

use libc::{c_char, malloc, strcpy}; use mbox::MString;

// Assume we have a C function that returns a malloc'ed string. unsafe extern "C" fn createstr() -> *mut cchar { let ptr = malloc(12) as *mut cchar; strcpy(ptr, b"Hello world\0".asptr() as *const c_char); ptr }

fn main() { // we wrap the null-terminated string into an MString. let string = unsafe { MString::fromrawunchecked(create_str()) };

// check the content is expected.
assert_eq!(&*string, "Hello world");

// the string will be dropped by `free` after the code is done.

} ```

Installation

Add this to your Cargo.toml:

toml [dependencies] mbox = "0.3"

Usage

This crate provides three main types, all of which uses the system's malloc/free as the allocator.

#![no_std]

You may compile mbox and disable the std feature to not link to std (it will still link to core.

toml [dependencies] mbox = { version = "0.3", default-features = false }

When #![no_std] is activated, you cannot convert an MString into a std::ffi::CStr, as the type simply does not exist 🙂.

Migrating from other crates

Note that MBox does not support custom allocator. If the type requires custom allocation, MBox cannot serve you.