]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-14919.rs
auto merge of #20578 : japaric/rust/no-more-bc, r=nmatsakis
[rust.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 #![feature(associated_types)]
12
13 trait Matcher {
14     fn next_match(&mut self) -> Option<(uint, uint)>;
15 }
16
17 struct CharPredMatcher<'a, 'b> {
18     str: &'a str,
19     pred: Box<FnMut(char) -> bool + 'b>,
20 }
21
22 impl<'a, 'b> Matcher for CharPredMatcher<'a, 'b> {
23     fn next_match(&mut self) -> Option<(uint, uint)> {
24         None
25     }
26 }
27
28 trait IntoMatcher<'a, T> {
29     fn into_matcher(self, &'a str) -> T;
30 }
31
32 impl<'a, 'b, F> IntoMatcher<'a, CharPredMatcher<'a, 'b>> for F where F: FnMut(char) -> bool + 'b {
33     fn into_matcher(self, s: &'a str) -> CharPredMatcher<'a, 'b> {
34         CharPredMatcher {
35             str: s,
36             pred: box self,
37         }
38     }
39 }
40
41 struct MatchIndices<M> {
42     matcher: M
43 }
44
45 impl<M: Matcher> Iterator for MatchIndices<M> {
46     type Item = (uint, uint);
47
48     fn next(&mut self) -> Option<(uint, uint)> {
49         self.matcher.next_match()
50     }
51 }
52
53 fn match_indices<'a, M, T: IntoMatcher<'a, M>>(s: &'a str, from: T) -> MatchIndices<M> {
54     let string_matcher = from.into_matcher(s);
55     MatchIndices { matcher: string_matcher }
56 }
57
58 fn main() {
59     let s = "abcbdef";
60     match_indices(s, |&mut: c: char| c == 'b')
61         .collect::<Vec<(uint, uint)>>();
62 }