Skip to content

Instantly share code, notes, and snippets.

@Geal
Geal / ibe.rs
Last active July 24, 2016 18:30
extern crate bn;
extern crate rand;
extern crate sodiumoxide;
mod sighash;
use bn::*;
use sodiumoxide::crypto::stream::{self,chacha20};
use std::str;
macro_rules! separated_list2 (
($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => (
{
let mut res = ::std::vec::Vec::new();
let mut input = $i;
// get the first element
let first = $submac!(input, $($args2)*);
if let $crate::IResult::Done(i,o) = first {
@Geal
Geal / test.rs
Created February 23, 2016 21:02
custom backtracking with nom
#[macro_use]
extern crate nom;
use nom::IResult;
macro_rules! n (
($name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => (
fn $name( i: $i ) -> std::result::Result<nom::IResult<$i,$o,u32>, nom::Err<$i, u32>> {
std::result::Result::Ok($submac!(i, $($args)*))
}
; ModuleID = 'nom_http.0.rs'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin"
%"9.nom::internal::IResult<&'static [u8], &'static [u8], u32>" = type { i64, [0 x i64], [5 x i64] }
%"2.core::ops::RangeTo<usize>" = type { i64 }
%"2.core::ops::Range<usize>" = type { i64, i64 }
%"9.nom::util::ErrorKind<u32>" = type { i32, [0 x i32], [1 x i32] }
%"2.core::ops::RangeFrom<usize>" = type { i64 }
%"9.nom::internal::Err<&'static [u8], u32>" = type { i8, [7 x i8], [4 x i64] }
@Geal
Geal / iteratees.rs
Last active September 13, 2015 19:00
nom iteratees
// Context:
// * Rust n'a pas de TCO, donc on ne peut pas faire de récursion facilement
// * la gestion de l'ownership fait que le résultat d'une passe sur le
// contenu du Producer ne vivra pas plus longtemps que son buffer (sauf si on cpone les données)
// * les Producers et Consumers actuels passent leur temps à copier des buffers,
// je veux limiter ça au Producer
type Computation<I,O,S,E> = Fn(S, Input<I>) -> (I,Consumer<I,O,S,E>);
enum Input<I> {
@Geal
Geal / nom_http.rs
Last active September 8, 2015 15:36
nom HTTP parsing benchmark (build with nom >= 0.4.0)
#![feature(test)]
extern crate test;
use test::Bencher;
#[macro_use]
extern crate nom;
use nom::IResult;
use std::env;
use std::fs::File;
pub enum Err<'a,P=&'a[u8]> {
Code(u32),
Node(u32,Box<Err<'a>>),
Position(u32,P)
}
pub enum IResult<'a,I,O> {
Done(I,O),
Error(Err<'a>)
}
@Geal
Geal / gist:22dd55c84a583b18ec30
Created August 13, 2015 08:53
Baremetal Rust
# What you need to write low level Rust code
* do not use cargo, use rustc directly to build your code
* you need to use a target.json file describing your target platform (example: https://github.com/hackndev/zinc/blob/master/thumbv6-none-eabi.json ). Pass it to rustc via --target=target.json
* you wan work without the standard library, by following those instructions: https://doc.rust-lang.org/book/no-stdlib.html
*
@Geal
Geal / gist:b9c97995371bffa44636
Last active August 29, 2015 14:23
state machine syntax
pub enum State {
A,
B(u8),
C(u8),
D
Error
}
enum Output {
X(u8),
@Geal
Geal / gist:6f10d739978bfbf96a10
Created February 9, 2015 15:12
Box<FnOnce> problems
pub type Fun<'a> = Box<FnOnce(u8) -> Res<'a> +'a>;
pub enum Res<'a> {
Done(u8, u8),
Incomplete(Fun<'a>)
}
pub trait FMap {
fn fmap<'y,F: Fn(u8) -> u8 + 'y>(self, f: &'y F) -> Res<'y>;
}