Skip to content

Instantly share code, notes, and snippets.

View phaylon's full-sized avatar

Robert »phaylon« Sedlacek phaylon

  • Hamburg, Germany
View GitHub Profile
#![feature(try_blocks, label_break_value)]
macro_rules! maybe {
($($t:tt)*) => {
'maybe_scope: loop {
break 'maybe_scope (try {
break 'maybe_scope {$($t)*}
})
}
}
fn deploy_metadata(tx: &rusqlite::Transaction) -> Result<(), Error> {
tx.execute("CREATE TABLE spire_metadata (version INTEGER NOT NULL)", rusqlite::NO_PARAMS)
.map_err(crate::database_error_context("deploy metadata table"))?;
tx.execute("INSERT INTO spire_metadata (version) VALUES (0)", rusqlite::NO_PARAMS)
.map_err(crate::database_error_context("populate metadata table"))?;
Ok(())
}
fn database_is_empty(tx: &rusqlite::Transaction) -> Result<bool, Error> {
let table_count: i64 = tx
@phaylon
phaylon / witness.rs
Created May 7, 2018 21:14
Rust Witness Types
trait Identity {}
struct Carrier;
impl Identity for Carrier {}
fn witness<T>(_: T) -> impl Identity { Carrier }
macro_rules! witness { () => { witness(|| {}) } }
fn main() {
@phaylon
phaylon / dont_do_this.rs
Created March 21, 2018 18:17
Custom Rust Operators
use std::ops;
fn main() {
let a = MyNum(23);
let b = MyNum(42);
println!("{:?}", a /plus/ b);
}
#[derive(Debug)]
struct MyNum(i64);
macro_rules! gen_strmapped_enum {
( $name:ident: $($variant:ident => $value:expr,)+ ) => {
#[derive( Copy, Clone, Eq, PartialEq, Debug )]
pub enum $name {
$( $variant, )+
}
impl $name {
fn from_str(value: &str) -> Option<$name> {
match value {
@phaylon
phaylon / gist:477a5e560ec51a936921
Created February 9, 2015 21:39
Iterator .into method with closure target
#![feature( core )]
#![allow( unused_mut )]
pub trait IterInto: Iterator {
type Item;
fn into<T>(mut self, target: T)
where T: IterIntoTarget<<Self as IterInto>::Item>;
}
#![feature(macro_rules)]
#![experimental]
use std::num::Zero;
#[deriving(Show, PartialEq, PartialOrd, Ord, Eq, Clone)]
pub enum CheckedErr {
WrapErr,
DivByZeroErr,
}
@phaylon
phaylon / perl-code.t
Created September 25, 2014 18:13
Basic construction/destruction of Rust objects from Perl 5
use strict;
use warnings;
use Test::More;
use File::Basename;
use FFI::Raw;
my $lib = join '/', dirname(__FILE__), 'rust-code';
system("rustc --crate-type dylib --opt-level 3 -o $lib.so $lib.rs")
and die "Aborting due to build failure\n";
@phaylon
phaylon / apply.rs
Last active August 29, 2015 14:03
General closure operations for all types
trait ApplyClosure {
fn tangent<'a>(mut self, op: |&mut Self|) -> Self {
op(&mut self);
self
}
fn apply<R>(self, op: |Self| -> R) -> R {
op(self)
}
#![crate_id = "check"]
#![crate_type = "bin"]
#![feature(macro_rules)]
#[deriving(Show)]
enum CheckedVariant<T> {
Checked(T),
NaN,
}