Skip to content

Instantly share code, notes, and snippets.

View aturon's full-sized avatar

Aaron Turon aturon

View GitHub Profile
@aturon
aturon / ProcessBuilder.rs
Created April 30, 2014 00:03
draft ProcessBuilder implementation
/// This process builder type provides fine-grained control over how a new
/// process should be spawned. A default configuration can be generated using
/// `ProcessBuilder::new(program)`, where `program` gives a path to the program
/// to be executed. Additional builder methods allow the configuration to be
/// changed (for example, by adding arguments) prior to spawning:
///
/// ```
/// use std:io::ProcessBuilder;
///
/// let mut process = match ProcessBuilder.new("sh").arg("-c").arg("echo hello").spawn() {
@aturon
aturon / mut-for-immutable.rs
Last active August 29, 2015 14:01
can add mut to self in trait impl
// The trait itself says the method takes self as immutable
trait ImmutableOp {
fn do_immutable(self);
}
impl ImmutableOp for Vec<u8> {
// FAILS TO COMPILE, AS IT SHOULD:
// fn do_immutable(self) {
// self.push(0);
// }
---- [run-pass] run-pass/while-cont.rs stdout ----
error: compilation failed!
status: exit code: 101
command: x86_64-apple-darwin/stage2/bin/rustc /Users/aturon/moz/working/issue-11650/src/test/run-pass/while-cont.rs -L x86_64-apple-darwin/test/run-pass --target=x86_64-apple-darwin -L x86_64-apple-darwin/test/run-pass/while-cont.stage2-x86_64-apple-darwin.libaux -C prefer-dynamic -o x86_64-apple-darwin/test/run-pass/while-cont.stage2-x86_64-apple-darwin --cfg rtopt --cfg debug -L x86_64-apple-darwin/rt
stdout:
------------------------------------------
task 'rustc' failed at 'needs a temp dir', /Users/aturon/moz/working/issue-11650/src/libstd/option.rs:245
---- [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:
------------------------------------------
task 'rustc' failed at 'needs a temp dir', /Users/aturon/moz/working/issue-11650/src/libstd/option.rs:245
task 'rustc' failed at 'needs a temp dir', /Users/aturon/moz/working/issue-11650/src/libstd/option.rs:245
stack backtrace:
1: 0x10c408f74 - rt::backtrace::imp::write::h5987cfa6d2cce788D5a::v0.11.pre
2: 0x10c35bcce - rt::unwind::begin_unwind_inner::hb4e6b4562dd8b47fHFa::v0.11.pre
3: 0x1096dc708 - rt::unwind::begin_unwind::h15535554073126010795::v0.11.pre
4: 0x109e0ebde - back::link::link_natively::ha20464d71015abc59B4::v0.11.pre
5: 0x109e08d4f - back::link::link_binary::h76c69827ffab817bDg4::v0.11.pre
6: 0x109f00209 - driver::driver::phase_6_link_output::h1b1589e6399fb3f7v7h::v0.11.pre
7: 0x109f0311a - driver::driver::compile_input::h3b5586ed143875c55mi::v0.11.pre
8: 0x109f29007 - run_compiler::h483ea1bd82c3ef35HQp::v0.11.pre
---- [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