|
Hyperon C
|
This documentation specifically covers the API for extending Hyperon with modules implemented in C as well as integrating Hyperon into a C project. For a more complete overview of Hyperon, you should look here: TODO: Where?
The HyperonC API is intended to be a light weight layer over the native Rust implementation, and is designed to impose as little runtime performance overhead as possible. It is not necessary to be familiar with Rust to use HyperonC, but it might clarify the paterns because some of the API semantics come directly from ownership and borrowing in Rust.
When a Hyperon object is created through the creation function, it is given to you, the caller, by value. For example atom_sym() returns an atom_t struct. You own that atom_t and you must free it when it's no longer needed, using the corresponding free function; atom_free() in the case of atom_t.
However, if you pass the object by value to another function, that function takes ownership and now you are no longer responsible for freeing the object. For example, atom_vec_push() takes an atom_t argument, passed by value. This means the passed atom is now owned by the vec, and it is the responsibility of the vec to free the atom.
Some functions return pointers or smart pointers (structs with types ending in "ref_t") pointing to other objects. When working with a pointer or ref_t smart pointer, you must be cognizant of the life cycle of the referenced object. For example, atom_vec_get() returns an atom_ref_t to refer to an atom within the vec. That reference will become invalid if the atom_vec_t the pointer refers into is freed or changed in any way. The Rust borrow checker ensures these constraints are never violated, but in C you must take that responsibility yourself.