CPP-RSTD
Very incomplete C++17 replica of Rust's std.
Usage
#include <rstd/prelude.hpp>
using namespace rstd;
// Your code
Naming
- Safe types and functions (that always provides defined result or panics) has usual names like
Option
ordrop
. - Unsafe types and function (that could result in UB in some circumstances) has a single underscore
_
before their name. Examples:_Union
,Option::_take_some
and others. - Internal entities names starts with double underscore
__
. They shouldn't be used externally because of unintended side-effects and possibility of change in future versions.
Types
Base types
-
_Union<Elems...>
- Templated analog of Cunion
. It can store non-trivial types and supports move semantics. InDEBUG
mode union panics on double initialization or double free (but not type mismatch). -
Variant<Elems...>
- Union with id of stored type. Similar to Rustenum
but with a significant difference - it also includes an empty (or none) state because of requirements of C++ move semantics. -
Tuple<Elems...>
- Sequence of objects of different types. The special case is empty tupleTuple<>
that is used as a placeholder when we need to deal with nothing. -
Option<T>
- Type that stores something or nothing. Similar to RustOption
. -
Result<T, E>
- Type that stores one value on success and another on error. Similar to RustResult
but with additional empty state - seeVariant
. Also inDEBUG
mode result panics if it wasn't explicitly handled - useResult::unwrap
orResult::clear
.
Memory managements
Box<T>
- Heap-located storage with ability to move the object inside and outside. Wrapper over C++std::unique_ptr
.Rc<T>
- Reference counting heap-located storage. Wrapper over C++std::shared_ptr
.- to be implemented.Weak<T>
Concurrency
Thread<T>
- POSIX-thread wrapper. Has its ownstdin_
,stdout_
andstderr_
and panic hook. In case of panic simply returns aErr
fromjoin
without causing the whole program to be terminated._Mutex
andMutex<T>
- POSIX-thread mutex. The second is the safe version of the first.Mutex<T>
wraps some value allowing to access it only with lock providingGuard
object that unlocks the mutex when going out of scope.
Functions
clone
- Makes explicit copy of object and returns it.move
- Moves the object. Unlikestd::move
it actually performs moving of object resources even if the result isn't passed anywhere.drop
- Makes object to release its resources and enter an empty state.
Macros
panic_
- Print formatted message and terminate thread.assert_
andassert_eq_
- Assertion macros, panics on false condition with an information of arguments.
Formatted output
format_
- Write to string.write_
andwriteln_
- Write to specifiedstd::ostream
.print_
andprintln_
- Write tostdout_()
.eprint_
andeprintln_
- Write tostderr_()
.
Format examples:
print_(); // > ""
print_(123); // > "123"
print_("abc"); // > "abc"
print_("a: {}, b: {};", 123, "abc"); // > "a: 123, b: abc;"
print_("{{abc}}"); // > "{abc}"
print_(std::string("{}")); // > "{}"
Testing
The library provides its own testing framework.
Usage
Define test cases in you multiple .cpp
files:
#include <rtest.hpp>
using namespace rstd;
rtest_module_(my_test) {
rtest_(dummy) {
}
rtest_should_panic_(panic) {
panic_("Panic!");
}
rtest_should_panic_(segfault) {
((void(*)())nullptr)();
}
}
And include the following header in your single main .cpp
file:
#include <rtest/main.hpp>
int main(int argc, char *argv[]) {
return rtest::main(argc, argv);
}