1 // Copyright (C) 2023-2024 Free Software Foundation, Inc.
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)
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.
25 use std::convert::TryInto;
30 /// An iterator over [`TokenStream`]'s [`TokenTree`]s.
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 {
44 let result = self.current;
45 self.current = unsafe { self.current.add(1) };
46 Some(unsafe { std::ptr::read(result) }.into())
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))
58 fn count(self) -> usize {
59 self.end as usize - self.current as usize
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();
71 end: unsafe { self.0.data.add(capacity) },