Skip to content

Instantly share code, notes, and snippets.

@abhikp
abhikp / nested_match.rs
Created October 12, 2017 19:39
Return a nested match where an error could also be returned
fn nested_match(unpack: Option<Option<i32>>) -> Result<Option<i32>, String> {
return Ok(match unpack {
None => None,
Some(option) => {
let unpack_again = match option {
Some(int) => int,
None => return Err("ERROR".to_owned()),
};
Some(unpack_again)
@abhikp
abhikp / assign_raw_pointer.rs
Last active October 11, 2017 16:09
Assign raw pointer for error handling according in FFI hourglass pattern
use std::ptr;
pub struct Error {
description: String,
}
pub unsafe extern "C" fn assign_error(error_t_t: *mut *mut Error) {
let error_ptr = Box::new(Error {
description: "This is an error".to_owned(),
});
@abhikp
abhikp / extend_trait.rs
Last active October 10, 2017 23:10
Extending traits in Rust
pub trait GenericError {
fn description(&self) -> String {
return "GenericError".to_owned();
}
}
pub trait Error: GenericError {
}
pub struct MyError {}