Skip to content

Instantly share code, notes, and snippets.

View aturon's full-sized avatar

Aaron Turon aturon

View GitHub Profile
---- [run-pass] run-pass/vec-matching.rs stdout ----
error: compilation failed!
status: exit code: 101
command: x86_64-apple-darwin/stage1/bin/rustc /Users/aturon/moz/working/issue-11650/src/test/run-pass/vec-matching.rs -L x86_64-apple-darwin/test/run-pass --target=x86_64-apple-darwin -L x86_64-apple-darwin/test/run-pass/vec-matching.stage1-x86_64-apple-darwin.libaux -C prefer-dynamic -o x86_64-apple-darwin/test/run-pass/vec-matching.stage1-x86_64-apple-darwin --cfg rtopt --cfg debug -L x86_64-apple-darwin/rt
stdout:
------------------------------------------
------------------------------------------
stderr:
@aturon
aturon / gist:c20615b26fb31c337afd
Created May 14, 2014 21:24
Places where custom discriminants are (possibly) used
./librand/distributions/gamma.rs:enum ChiSquaredRepr {
// k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
// e.g. when alpha = 1/2 as it would be for this case, so special-
// casing and using the definition of N(0,1)^2 is faster.
DoFExactlyOne,
DoFAnythingElse(Gamma),
}
./librustc/lib/llvm.rs:pub enum CallConv {
CCallConv = 0,
FastCallConv = 8,
@aturon
aturon / gist:8e6d72cf5e3779f84e7c
Created May 15, 2014 18:18
Commentary on return types

Currently, APIs are often forced to either (1) expose a concrete return type or (2) return trait objects, which come with several downsides.

For example, the iter function for Vec<T> currently has the signature

fn iter<'a>(&'a self) -> Items<'a, T>

where Items is a struct defined purely for iterating over a slice. The struct has no public fields; the only interesting thing about it is that it implements various traits, such as Iterator<&'a T>.

This API is revealing more than it might have to: the type Items should not have to be exposed at all. Instead, we should be able to give iter a signature like:

@aturon
aturon / cow.rs
Last active August 29, 2015 14:01
Copy-on-write smart pointer
// A copy-on-write smart pointer
// See also http://stlab.adobe.com/classadobe_1_1version__1_1_1copy__on__write.html
enum CowCell<'a, T> {
Shared(&'a T),
Owned(T)
}
pub struct Cow<'a, T> {
cell: CowCell<'a, T>
@aturon
aturon / rc-cow.rs
Created May 29, 2014 17:47
clone-on-write for Rc
impl<T: Clone> Rc<T>
#[inline(always)]
fn inner_mut<'a>(&'a mut self) -> &'a mut RcBox<T> {
unsafe { &mut (*self._ptr) }
}
/// Construct a mutable pointer by cloning the referent into a new
/// reference-counted box, unless this is the sole reference.
pub fn to_mut<'a>(&'a mut self) -> &'a mut T {
// We hold a strong reference, which is also implicitly a weak
C:\te;st>cp ..\test\testrust.exe .
cp: `..\\test\\testrust.exe' and `./..\\test\\testrust.exe' are the same file
C:\te;st>
@aturon
aturon / gist:f5589dcb1d7702f33239
Created June 5, 2014 17:05
stability inheritance questions
#[deprecated="This module was a bad idea"]
pub mod outer {
#[stable]
pub mod inner {
#[unstable]
pub fn foo() {}
pub fn bar() {}
}
@aturon
aturon / cowstring.rs
Last active August 29, 2015 14:02
Unifying String and &str with clone-on-write
// An attempt at unifying String and &str into a single type, String<'a>
// String => String<'static>
// &'a str => String<'a>
//
// I've tried to reimplement some of the String and &str interface as a proof of
// concept.
//
// Doc comments are from existing API, but I've marked interesting notes with a
// comment with double **
//
fn test_stderr() {
let (tx, rx) = channel();
let mut reader = ChanReader::new(rx);
let stderr = ChanWriter::new(tx);
let res = build().stderr(box stderr as Box<Writer + Send>).try(proc() -> () {
fail!("Hello, world!")
});
assert!(res.is_err())
@aturon
aturon / HKT-via-AI.rs
Last active August 29, 2015 14:04
Encoding HKT via first-order associated types and where clauses
// Encoding of HKTs
//
// We use a single trait for all types inhabiting higher kinds.
// We implement the trait on tuples (Selector, Input1, ..., InputN),
// where the "Selector" type _names_ the actual type we want, indirectly.
trait HKT {
type Output;
}
// Some example selectors -- just fresh types: