#nul #transparent #c-str #no-alloc

no-std seestr

pointer-wide nul-terminated strings with ownership semantics

6 releases

0.1.5 Nov 29, 2024
0.1.4 Nov 28, 2024

#363 in FFI

Download history 377/week @ 2024-11-25 69/week @ 2024-12-02

446 downloads per month

Apache-2.0 OR MIT

19KB
385 lines

Pointer-wide nul-terminated strings for use in FFI.

The following C API:

char *create(void); // may return nul
void destroy(char *);
char *get_name(struct has_name *); // may return nul
char *version(void); // never null

Can be transcribed as follows:

extern "C" {
    fn create() -> Option<Buf>;
    fn destroy(_: Buf);
    fn get_name(_: &HasName) -> Option<&NulTerminated>;
    fn version() -> &'static NulTerminated;
}

As opposed to:

extern "C" {
    fn create() -> *mut c_char;
    fn destroy(_: *mut c_char);
    fn get_name(_: *mut HasName) -> *mut c_char;
    fn version() -> *mut c_char;
}

lib.rs:

Pointer-wide nul-terminated strings for use in FFI.

The following C API:

char *create(void); // may return nul
void destroy(char *);
char *get_name(struct has_name *); // may return nul
char *version(void); // never null

Can be transcribed as follows:

extern "C" {
    fn create() -> Option<Buf>;
    fn destroy(_: Buf);
    fn get_name(_: &HasName) -> Option<&NulTerminated>;
    fn version() -> &'static NulTerminated;
}

As opposed to:

extern "C" {
    fn create() -> *mut c_char;
    fn destroy(_: *mut c_char);
    fn get_name(_: *mut HasName) -> *mut c_char;
    fn version() -> *mut c_char;
}

Dependencies

~43KB