minicoro_sys/
lib.rs

1// Autogenerated binding to minicoro C library
2
3#![allow(unused, non_camel_case_types, improper_ctypes)]
4
5use core::ffi::c_char;
6
7pub const MCO_DEFAULT_STORAGE_SIZE: usize = 1024;
8pub const MCO_MIN_STACK_SIZE: usize = 32768;
9pub const MCO_DEFAULT_STACK_SIZE: usize = 57344;
10
11#[repr(C)]
12pub struct mco_coro;
13
14#[repr(C)]
15#[derive(Debug, PartialEq, Eq)]
16pub enum mco_result {
17    MCO_SUCCESS = 0,
18    MCO_GENERIC_ERROR,
19    MCO_INVALID_POINTER,
20    MCO_INVALID_COROUTINE,
21    MCO_NOT_SUSPENDED,
22    MCO_NOT_RUNNING,
23    MCO_MAKE_CONTEXT_ERROR,
24    MCO_SWITCH_CONTEXT_ERROR,
25    MCO_NOT_ENOUGH_SPACE,
26    MCO_OUT_OF_MEMORY,
27    MCO_INVALID_ARGUMENTS,
28    MCO_INVALID_OPERATION,
29    MCO_STACK_OVERFLOW,
30}
31
32#[repr(C)]
33#[derive(Debug, PartialEq, Eq)]
34pub enum mco_state {
35    MCO_DEAD = 0, /* The coroutine has finished normally or was uninitialized before finishing. */
36    MCO_NORMAL, /* The coroutine is active but not running (that is, it has resumed another coroutine). */
37    MCO_RUNNING, /* The coroutine is active and running. */
38    MCO_SUSPENDED, /* The coroutine is suspended (in a call to yield, or it has not started running yet). */
39}
40
41#[repr(C)]
42pub struct mco_desc {
43    pub func: fn(co: *const mco_coro), /* Entry point function for the coroutine. */
44    pub user_data: *const (),          /* Coroutine user data, can be get with `mco_get_user_data`. */
45    /* Custom allocation interface. */
46    pub malloc_cb: unsafe extern "C" fn(size: usize, allocator_data: *const ()) -> *const (), /* Custom allocation function. */
47    pub free_cb: unsafe extern "C" fn(ptr: *const (), allocator_data: *const ()), /* Custom deallocation function. */
48    pub allocator_data: *const (), /* User data pointer passed to `malloc`/`free` allocation functions. */
49    pub storage_size: usize,       /* Coroutine storage size, to be used with the storage APIs. */
50    /* These must be initialized only through `mco_init_desc`. */
51    pub coro_size: usize,  /* Coroutine structure size. */
52    pub stack_size: usize, /* Coroutine stack size. */
53}
54
55#[link(name = "minicoro", kind = "static")]
56extern "C" {
57    pub fn mco_desc_init(func: extern "C" fn(co: *const mco_coro), stack_size: usize) -> mco_desc; /* Initialize description of a coroutine. When stack size is 0 then MCO_DEFAULT_STACK_SIZE is used. */
58    pub fn mco_init(co: *const mco_coro, desc: *const mco_desc) -> mco_result; /* Initialize the coroutine. */
59    pub fn mco_uninit(co: *const mco_coro) -> mco_result; /* Uninitialize the coroutine, may fail if it's not dead or suspended. */
60    pub fn mco_create(out_co: *mut *const mco_coro, desc: *const mco_desc) -> mco_result; /* Allocates and initializes a new coroutine. */
61    pub fn mco_destroy(co: *const mco_coro) -> mco_result; /* Uninitialize and deallocate the coroutine, may fail if it's not dead or suspended. */
62    pub fn mco_resume(co: *const mco_coro) -> mco_result; /* Starts or continues the execution of the coroutine. */
63    pub fn mco_yield(co: *const mco_coro) -> mco_result; /* Suspends the execution of a coroutine. */
64    pub fn mco_status(co: *const mco_coro) -> mco_state; /* Returns the status of the coroutine. */
65    pub fn mco_get_user_data(co: *const mco_coro) -> *const u8; /* Get coroutine user data supplied on coroutine creation. */
66
67    /* Storage interface functions, used to pass values between yield and resume. */
68    pub fn mco_push(co: *const mco_coro, src: *const u8, len: usize) -> mco_result; /* Push bytes to the coroutine storage. Use to send values between yield and resume. */
69    pub fn mco_pop(co: *const mco_coro, dest: *const u8, len: usize) -> mco_result; /* Pop bytes from the coroutine storage. Use to get values between yield and resume. */
70    pub fn mco_peek(co: *const mco_coro, dest: *const u8, len: usize) -> mco_result; /* Like `mco_pop` but it does not consumes the storage. */
71    pub fn mco_get_bytes_stored(co: *const mco_coro) -> usize; /* Get the available bytes that can be retrieved with a `mco_pop`. */
72    pub fn mco_get_storage_size(co: *const mco_coro) -> usize; /* Get the total storage size. */
73
74    /* Misc functions. */
75    pub fn mco_running() -> *const mco_coro; /* Returns the running coroutine for the current thread. */
76    pub fn mco_result_description(res: mco_result) -> *const c_char; /* Get the description of a result. */
77}