Skip to content

Instantly share code, notes, and snippets.

@tomjakubowski
Forked from japaric/Makefile
Last active November 13, 2015 17:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjakubowski/9c49af9cd2582c37d0e8 to your computer and use it in GitHub Desktop.
Save tomjakubowski/9c49af9cd2582c37d0e8 to your computer and use it in GitHub Desktop.
#![crate_type = "rlib"]
#![no_std]
#![feature(lang_items, unboxed_closures)]
pub mod kinds {
#[lang = "sized"]
pub trait Sized { }
#[lang = "copy"]
pub trait Copy { }
}
pub mod ops {
use kinds::Sized;
#[lang = "fn"]
pub trait Fn<Args, Result> {
extern "rust-call" fn call(&self, args: Args) -> Result;
}
#[lang = "fn_mut"]
pub trait FnMut<Args, Result> {
extern "rust-call" fn call_mut(&mut self, args: Args) -> Result;
}
impl <F: ?Sized, A, R> FnMut<A, R> for F where F: Fn<A, R> {
extern "rust-call" fn call_mut(&mut self, args: A) -> R {
self.call(args)
}
}
}
pub mod iter {
use kinds::Sized;
use ops::FnMut;
use option::Option;
pub trait Iterator<A> : Sized {
fn next(&mut self) -> Option<A>;
}
pub trait IteratorExt<A>: Iterator<A> {
fn map<B, F>(self, _: F) -> Map<A, B, Self, F> where F: FnMut(A) -> B {
loop {}
}
fn fold<B, F>(self, _: B, _: F) -> B where F: FnMut(B, A) -> B {
loop {}
}
}
impl <A, I> IteratorExt<A> for I where I: Iterator<A> { }
pub trait AdditiveIterator<A> {
fn sum(self) -> A;
}
impl<I> AdditiveIterator<uint> for I where I: Iterator<uint> {
// XXX This definition is required
fn sum(self) -> uint {
self.fold(0, |acc, x| acc + x)
}
}
pub struct Map<A, B, I: Iterator<A>, F: FnMut(A) -> B>;
impl <A, B, I, F> Iterator<B> for Map<A, B, I, F> where I: Iterator<A>, F: FnMut(A) -> B {
fn next(&mut self) -> Option<B> {
loop {}
}
}
}
pub mod option {
pub enum Option<T> {}
}
pub mod slice {
use iter::Iterator;
use option::Option;
pub struct Items<'a, T: 'a>;
impl <'a, T> Iterator<&'a T> for Items<'a, T> {
fn next(&mut self) -> Option<&'a T> {
loop {}
}
}
}
pub mod str {
use iter::Map;
use slice;
pub type Bytes<'a> = Map<&'a u8, u8, slice::Items<'a, u8>, fn(&u8) -> u8>;
pub trait StrPrelude {
fn bytes(&self) -> Bytes;
}
impl StrPrelude for str {
fn bytes(&self) -> Bytes {
loop {}
}
}
}
test:
rm -f liberoc.rlib
rustc eroc.rs
rustc -L . -C codegen_units=2 unicode.rs
rustc: /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/llvm/lib/IR/Constants.cpp:1609: static llvm::Constant* llvm::ConstantExpr::getPointerCast(llvm::Constant*, llvm::Type*): Assertion `S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"' failed.
#![crate_type = "rlib"]
#![no_std]
#![feature(unboxed_closures)]
extern crate eroc;
use eroc::iter::{AdditiveIterator, IteratorExt};
use eroc::str::StrPrelude;
pub trait UnicodeStrPrelude {
fn width(&self) -> uint;
}
impl UnicodeStrPrelude for str {
// XXX This `#[inline]` is the trigger
#[inline]
fn width(&self) -> uint { self.bytes().map(|_| 0).sum() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment