Created
September 27, 2016 00:46
-
-
Save anonymous/f4f2e1335a47df23eebb332b36e12674 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(unboxed_closures)] | |
#![feature(fn_traits)] | |
#[derive(Clone)] | |
struct Pr { | |
pat: i32 //, mat: Matcher<'a,'a> | |
} | |
type MState = i32; | |
impl Pr { | |
fn go(&self, x : &i32) -> Box<Iterator<Item=MState>> { | |
//let a = self.mat.states(&self.pat, x); | |
let a = vec!().into_iter(); | |
//let c = b.chain(iter::once(x.clone())); | |
let d : Box<Iterator<Item=MState>> = Box::new(a); | |
d | |
} | |
} | |
fn err() { | |
let a = vec!().into_iter(); | |
//let b = a.flat_map(move |x| self.go(&x)); | |
let b = a.map(Pr{pat:1}.clone()); | |
} | |
// as FnMut<_,Output=Box<Iterator<Item=MState>>> | |
impl FnOnce<(MState,)> for Pr { | |
type Output = Box<Iterator<Item=MState>>; | |
extern "rust-call" fn call_once(self, (args,): (MState,)) -> Self::Output { | |
self.go(&args) | |
} | |
} | |
impl FnMut<(MState,)> for Pr { | |
extern "rust-call" fn call_mut(&mut self, (args,): (MState,)) -> Self::Output { | |
self.go(&args) | |
} | |
} | |
impl FnOnce<MState> for Pr { | |
type Output = Box<Iterator<Item=MState>>; | |
extern "rust-call" fn call_once(self, args: MState) -> Self::Output { | |
self.go(&args) | |
} | |
} | |
impl FnMut<MState> for Pr { | |
extern "rust-call" fn call_mut(&mut self, args: MState) -> Self::Output { | |
self.go(&args) | |
} | |
} | |
use std::borrow::Cow; | |
#[derive(Debug)] | |
struct I<'a>(&'a i32); | |
impl<'a> Clone for I<'a> { | |
fn clone(&self) -> I<'a> { | |
println!("Cloning {:?}", self); | |
I(self.0) | |
} | |
} | |
type CowStr<'a> = Cow<'a, str>; | |
type StaticCowStr = Cow<'static, str>; // Either an owned string or a static string | |
fn is_owned<A: ?Sized>(x : &Cow<A>) -> bool where A : ToOwned { | |
match *x { | |
Cow::Owned(_) => true, | |
Cow::Borrowed(_) => false | |
} | |
} | |
fn into_owned<A: ?Sized>(x : Cow<A>) -> Cow<'static,A> where A : ToOwned { | |
Cow::Owned(x.into_owned()) | |
} | |
fn to_cow(x : &str) -> CowStr { | |
Cow::Borrowed(x) | |
} | |
struct Uz { | |
i : i32, | |
y : [i32] | |
} | |
fn main() { | |
let mut str : Cow<'static,str> = Cow::Borrowed("Hello"); | |
println!("{:?}, {:?}", str, is_owned(&str)); | |
str.to_mut().push_str(" World!"); | |
println!("{:?}, {:?}", str, is_owned(&str)); | |
let _strs : Vec<CowStr> = str.split(" ").map(to_cow).collect(); | |
let _strs2 : Vec<StaticCowStr> = "Hello world!".split(" ").map(to_cow).collect(); | |
let _str2 : StaticCowStr = into_owned(str.clone()); | |
println!("{:?}", _strs[0] == "Hello"); | |
let v: Vec<_> = (0..4).collect(); | |
// let a : i32 = v.iter().map(I).collect(); | |
let mut v2 = vec![]; | |
// let xx = v.iter().next().unwrap(); | |
for x in &v { | |
v2.push(I(x)); | |
println!("{:?}", x); | |
} | |
let mut v3: Vec<&I> = vec![]; | |
for x in &v2 { | |
v3.push(&x); | |
} | |
v3.clone(); | |
// let a : Vec<&i32> = v.iter().collect(); | |
// let b : &[_] = (&a[0..2]).clone(); | |
// println!("{:?}, {:?}", a, a ); | |
} | |
/*Sequence([Optional([PatAtom(Long("enable-cxx-namespaces")), | |
PatAtom(Long("help")), | |
PatAtom(Long("no-unstable-rust")), | |
PatAtom(Short('o')), | |
PatAtom(Long("use-msvc-mangling")), | |
PatAtom(Long("override-enum-type")), | |
PatAtom(Long("no-type-renaming")), | |
PatAtom(Long("no-class-constants")), | |
PatAtom(Long("emit-clang-ast")), | |
PatAtom(Long("no-bitfield-methods")), | |
PatAtom(Long("ignore-functions")), | |
PatAtom(Long("ignore-methods")), | |
PatAtom(Long("builtins")), | |
PatAtom(Long("allow-unknown-types")), | |
PatAtom(Long("no-namespaced-constants"))]), | |
Optional([Repeat(PatAtom(Long("link")))]), | |
Optional([Repeat(PatAtom(Long("static-link")))]), | |
Optional([Repeat(PatAtom(Long("framework-link")))]), | |
Optional([Repeat(PatAtom(Long("match")))]), | |
Optional([Repeat(PatAtom(Long("raw-line")))]), | |
Optional([Repeat(PatAtom(Long("dtor-attr")))]), | |
Optional([Repeat(PatAtom(Long("opaque-type")))]), | |
Optional([Repeat(PatAtom(Long("blacklist-type")))]), | |
PatAtom(Positional("input-header")), | |
Optional([PatAtom(Command("--")), Repeat(PatAtom(Positional("clang-args")))])]); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment