Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jamesmunns/optional_c
Implementation of Rust's Optional Type in C
https://github.com/jamesmunns/optional_c
Last synced: 11 days ago
JSON representation
Implementation of Rust's Optional Type in C
- Host: GitHub
- URL: https://github.com/jamesmunns/optional_c
- Owner: jamesmunns
- License: mit
- Created: 2015-10-21T21:01:57.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-22T21:14:24.000Z (about 9 years ago)
- Last Synced: 2024-10-09T07:10:47.222Z (30 days ago)
- Language: C
- Size: 148 KB
- Stars: 17
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# optional_c
## Implementation of Rust's Optional Type in CThis is a terrible proof of concept, attempting to bring the functionality of Rust's Optional Type (among many other languages) to C, through the magic of macros.
You can use it with standard types, your own types, structure definitions, the usages are limitless! Overhead is limited to two bytes (plus whatever your compiler packs structures to), and a little bit of sanity.
This brings the benefit of forcing the user to check the pass/fail state of a function. They cannot ignore the return code, and use the possibly undefined contents.
I even threw some Error Code possiblilties, but no behavior on that yet.
I accept pull requests.
# Usage
```C
#include "optional_c.h"// Define your optional type
DefineOptional(float);// Make a function that returns a float
Optional(float) goodExample( void )
{
return Some(float, 1.0f);
}// Make a function that doesnt (really) return a float
Optional(float) badExample( void )
{
return None(float);
}void testFunction(void)
{
Optional(float) myOptionalFloat;
float actualFloat;myOptionalFloat = goodExample();
if(Unwrap(myOptionalFloat, actualFloat))
{
printf("%.1f\n", actualFloat); // Prints "1.0"
}myOptionalFloat = badExample();
if(IsNone(myOptionalFloat))
{
printf("Guess that didn't go well...\n"); // It didnt go well!
}
}
```# Terrible things done by this implementation
Included but not limited to:* Use of the `##` macro concatenator
* Use of the `,` operator# Known Issues
* You cannot `DefineOptional(type)` twice. It will cause lots of compiler issues. Instead, create a single `OptionalTypes.h`.
* Creating an `Optional(type)` instance doesn't initialize it. This means it will probably be seen as a `Some(type)` if you don't zero it first. Workarounds:
* `Optional(type) foo = None(type);`
* `Optional(type) foo = {{0}};`