#memory #io #ffi #memory-address

unsaferawbuf

Convenient interface to a portion of manually allocated memory

3 releases

0.1.2 May 18, 2024
0.1.1 May 18, 2024
0.1.0 Oct 12, 2022

#192 in #memory

Download history 7/week @ 2024-02-22 7/week @ 2024-02-29 10/week @ 2024-03-28 5/week @ 2024-04-04 1/week @ 2024-04-11 30/week @ 2024-04-18 203/week @ 2024-05-16

203 downloads per month

Zlib license

6KB
80 lines

UnsafeRawBuf

GitHub crates.io badge Docs.rs rustc requirements

unsaferawbuf provides a interface to a raw chunk of allocated memory. Intended for usage with libraries or applications that can typically only communicate over via a shared memory region. Thus, making it very unsafe, and the crate it's self, provides very little safety guarantees. This simply acts as a container around a raw memory address, and does pointer arithmetic around it when reading and writing data to it. Allowing for some additional convenience when transacting from an arbitrary memory address is unavoidable.

Usage

Right now, you can only initialize a UnsafeRawBuf from a raw memory address. In the future, you'll be able to create a freshly allocated memory buffer along with it's associated interface. As of now, that is currently impossible, so for now we have to manually allocate memory, then assign it to the container.

use std::ffi::c_void;
use unsaferawbuf::UnsafeRawBuf;

pub unsafe extern "C" fn dmp(addr: *mut c_void) {
    let buf = UnsafeRawBuf::from_address(addr as _);
    buf.write(42);
}

No runtime deps