Daily bump.
[official-gcc.git] / libgrust / libproc_macro / token_stream.rs
blob1b2ff0671e278212882650e24f1d8554832f965f
1 // Copyright (C) 2023-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU Proc Macro Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
14 // Under Section 7 of GPL version 3, you are granted additional
15 // permissions described in the GCC Runtime Library Exception, version
16 // 3.1, as published by the Free Software Foundation.
18 // You should have received a copy of the GNU General Public License and
19 // a copy of the GCC Runtime Library Exception along with this program;
20 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
21 // <http://www.gnu.org/licenses/>.
23 //! Public implementation details for the `TokenStream` type, such as iterators.
24 use bridge;
25 use std::convert::TryInto;
27 use TokenStream;
28 use TokenTree;
30 /// An iterator over [`TokenStream`]'s [`TokenTree`]s.
31 #[derive(Clone)]
32 pub struct IntoIter {
33     current: *const bridge::token_stream::TokenTree,
34     end: *const bridge::token_stream::TokenTree,
37 impl Iterator for IntoIter {
38     type Item = TokenTree;
40     fn next(&mut self) -> Option<TokenTree> {
41         if self.current == self.end {
42             None
43         } else {
44             let result = self.current;
45             self.current = unsafe { self.current.add(1) };
46             Some(unsafe { std::ptr::read(result) }.into())
47         }
48     }
50     fn size_hint(&self) -> (usize, Option<usize>) {
51         // TODO: I'm not a fan of those casts, once #![feature(ptr_sub_ptr)]
52         // is implemented we may replace this line by the following:
53         // self.end.sub_ptr(self.current)
54         let remaining = self.end as usize - self.current as usize;
55         (remaining, Some(remaining))
56     }
58     fn count(self) -> usize {
59         self.end as usize - self.current as usize
60     }
63 impl IntoIterator for TokenStream {
64     type Item = TokenTree;
65     type IntoIter = IntoIter;
67     fn into_iter(self) -> IntoIter {
68         let capacity = self.0.size.try_into().unwrap();
69         IntoIter {
70             current: self.0.data,
71             end: unsafe { self.0.data.add(capacity) },
72         }
73     }