Skip to content

Instantly share code, notes, and snippets.

//! Monad chain macro in Rust
//!
//! A typical usage is to avoid nested match expressions.
//!
//! Supports:
//! - Pattern matching
//! - Or expressions (A | B => ...)
//! - Guarded statements (x if <cond> => ...)
#[feature(macro_rules)];
@jfager
jfager / Today.java
Last active February 18, 2022 06:56
Java 8 - Wrap Checked Exceptions in RuntimeExceptions Less Verbosely
package scratch.unsafe;
import java.util.Random;
public class Today {
public static void main(String[] args) {
String foo;
try {
if(new Random().nextBoolean()) {
@jfager
jfager / if_ok_all.rs
Last active August 29, 2015 13:56
Avoiding if_ok! repetition with a macro
#[feature(macro_rules)];
extern mod std;
use std::io::IoResult;
macro_rules! if_ok_all(
//Note that the Err case in these just returns, b/c this little demo is just running
//from main and so can't return an IoResult.
//#[no_uv]; //Works as expected w/ no_uv
extern mod std;
use std::{rand, task, vec};
use std::rand::Rng;
use std::hashmap::HashMap;
use std::task::TaskBuilder;
@jfager
jfager / msum.rs
Last active December 31, 2015 07:39
extern mod std;
extern mod extra;
use std::{num, util};
fn msum_10927(vals: &[f64]) -> f64 {
let mut partials : ~[f64] = ~[];
// `partials` is already borrowed when looping
// use `modifications` to update `partials`
@jfager
jfager / multicast.rs
Last active December 21, 2015 03:48
Playing around w/ trying to implement multicast.
use std::task;
use std::task::TaskBuilder;
enum MultiCastMsgChan<T> {
Msg(T),
MsgChan(Chan<T>)
}
struct MultiCast<T> {
priv ch: Chan<MultiCastMsgChan<T>>,
@jfager
jfager / fixed_len_vec_macros.rs
Last active June 16, 2019 17:36
Fun w/ rust macros - easily impl traits for fixed-length vectors.
extern mod std;
struct Foo([u8,..2]);
struct Bar([u8,..4]);
macro_rules! fixed_iter_bytes(
($t:ty) => (
impl IterBytes for $t {
fn iter_bytes(&self, lsb0: bool, f: std::to_bytes::Cb) -> bool {
self.as_slice().iter_bytes(lsb0, f)
fn ip6_to_str(ip6: &[u8]) -> ~str {
let f = u8::to_str_radix;
return fmt!("%s%s:%s%s:%s%s:%s%s:%s%s:%s%s:%s%s:%s%s",
f(ip6[ 0], 16), f(ip6[ 1], 16),
f(ip6[ 2], 16), f(ip6[ 3], 16),
f(ip6[ 4], 16), f(ip6[ 5], 16),
f(ip6[ 6], 16), f(ip6[ 7], 16),
f(ip6[ 8], 16), f(ip6[ 9], 16),
f(ip6[10], 16), f(ip6[11], 16),
f(ip6[12], 16), f(ip6[13], 16),
#include<stdio.h>
main() {
long long a = -9223372036854775808;
long long b = a * -1;
printf("%lld\n", b);
}
@jfager
jfager / segfaulting_server.rs
Last active December 18, 2015 13:09
Run segfaulting_server with 'rustc segfaulting_server.rs && export RUST_LOG=extra::net_tcp=debug && ./segfaulting_server', then run the client. The server will segfault after the 3rd client request. If you comment out lines 43 & 44, everything works great.
extern mod extra;
use std::{int,io,result,task};
use extra::{net,net_tcp,uv};
fn server(outer_ch: Chan<Chan<~str>>) {
let (accept_port, accept_chan) = stream();
let (finish_port, finish_chan) = stream();
let addr = extra::net_ip::v4::parse_addr("127.0.0.1");