]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/issue-14919.rs
933e7e40f06d34ce23ae96de299da6a594acb62b
[rustc.git] / src / test / run-pass / issue-14919.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(unknown_features)]
12 #![feature(box_syntax)]
13
14 trait Matcher {
15 fn next_match(&mut self) -> Option<(uint, uint)>;
16 }
17
18 struct CharPredMatcher<'a, 'b> {
19 str: &'a str,
20 pred: Box<FnMut(char) -> bool + 'b>,
21 }
22
23 impl<'a, 'b> Matcher for CharPredMatcher<'a, 'b> {
24 fn next_match(&mut self) -> Option<(uint, uint)> {
25 None
26 }
27 }
28
29 trait IntoMatcher<'a, T> {
30 fn into_matcher(self, &'a str) -> T;
31 }
32
33 impl<'a, 'b, F> IntoMatcher<'a, CharPredMatcher<'a, 'b>> for F where F: FnMut(char) -> bool + 'b {
34 fn into_matcher(self, s: &'a str) -> CharPredMatcher<'a, 'b> {
35 CharPredMatcher {
36 str: s,
37 pred: box self,
38 }
39 }
40 }
41
42 struct MatchIndices<M> {
43 matcher: M
44 }
45
46 impl<M: Matcher> Iterator for MatchIndices<M> {
47 type Item = (uint, uint);
48
49 fn next(&mut self) -> Option<(uint, uint)> {
50 self.matcher.next_match()
51 }
52 }
53
54 fn match_indices<'a, M, T: IntoMatcher<'a, M>>(s: &'a str, from: T) -> MatchIndices<M> {
55 let string_matcher = from.into_matcher(s);
56 MatchIndices { matcher: string_matcher }
57 }
58
59 fn main() {
60 let s = "abcbdef";
61 match_indices(s, |c: char| c == 'b')
62 .collect::<Vec<(uint, uint)>>();
63 }