Result Rt¶
| Spice | |
|---|---|
Result<T> struct¶
Result in Spice are wrappers around values, that allow to either provide a value or an error object. This is useful for functions that return a value, but also can fail. The error can then be dealt with on the caller side e.g. using the panic builtin.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a successful result holding the given data
Parameters
| Name | Type | Description |
|---|---|---|
data |
const T& |
Data value to wrap |
ctor¶
| Spice | |
|---|---|
Construct a failed result holding the given error
Parameters
| Name | Type | Description |
|---|---|---|
error |
const Error& |
Error to wrap |
Methods¶
unwrap¶
| Spice | |
|---|---|
Returns the stored data object. If an error is present, this function will panic.
The returned reference is borrowed - the result stays the owner of the data and destructs it when it goes out of scope. To take ownership of a heap payload, move it out of the result: heap byte* p = result.unwrapAndMove();
Returns: T&
unwrapOr¶
| Spice | |
|---|---|
If no error is present, return the stored data object, otherwise the given value.
Parameters
| Name | Type | Description |
|---|---|---|
alternativeValue |
const T& |
Alternative data value for the error case |
Returns: T&
unwrapOrElse¶
| Spice | |
|---|---|
If no error is present, return the stored data object, otherwise invoke the given callback.
Parameters
| Name | Type | Description |
|---|---|---|
callback |
f<T&>(const Error&) |
Callback which is invoked in case of an error |
Returns: T&
unwrapAndMove¶
| Spice | |
|---|---|
Move the stored data object out of the result object If an error is present, this function will panic.
Returns: T
getErr¶
| Spice | |
|---|---|
Return the enclosed error object.
Returns: Error&
isOk¶
| Spice | |
|---|---|
Checks if the result contains any data.
Returns: bool
isErr¶
| Spice | |
|---|---|
Checks if the result contains an error.
Returns: bool
dumpTrace¶
| Spice | |
|---|---|
Prints the recorded error return trace to stderr. Forwards to the enclosed error's dumpTrace(), see there.
Functions¶
ok¶
| Spice | |
|---|---|
Returns a result object with a value and no error.
Parameters
| Name | Type | Description |
|---|---|---|
data |
const T& |
Returns: Result<T>
err¶
| Spice | |
|---|---|
Returns a result object with an error and no value.
Parameters
| Name | Type | Description |
|---|---|---|
error |
const Error& |
Returns: Result<T>
err¶
| Spice | |
|---|---|
Returns a result object with an error and no value.
Parameters
| Name | Type | Description |
|---|---|---|
code |
int |
|
message |
string |
Returns: Result<T>
err¶
| Spice | |
|---|---|
Returns a result object with an error and no value.
Parameters
| Name | Type | Description |
|---|---|---|
message |
string |
Returns: Result<T>