public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] libproc_macro: Implement FromIterator
@ 2023-04-06 21:31 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:31 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:f84d6127fe60a968bf331ebb9cec58b16df86852

commit f84d6127fe60a968bf331ebb9cec58b16df86852
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Mon Feb 20 12:46:23 2023 +0100

    libproc_macro: Implement FromIterator
    
    Implement FromIterator for TokenStream, from either a TokenTree iterator
    or a TokenStream iterator.
    
    ChangeLog:
    
            * librust/proc_macro/rust/bridge/token_stream.rs: Add internal
            implementation.
            * librust/proc_macro/rust/lib.rs: Add FromIterator
            implementation.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 librust/proc_macro/rust/bridge/token_stream.rs | 52 ++++++++++++++++++++++++++
 librust/proc_macro/rust/lib.rs                 |  8 ++--
 2 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/librust/proc_macro/rust/bridge/token_stream.rs b/librust/proc_macro/rust/bridge/token_stream.rs
index 307ff6bd8b4..7135a0fb81c 100644
--- a/librust/proc_macro/rust/bridge/token_stream.rs
+++ b/librust/proc_macro/rust/bridge/token_stream.rs
@@ -1,4 +1,8 @@
 use bridge::{group::Group, ident::Ident, literal::Literal, punct::Punct};
+use std::convert::TryInto;
+
+type ExternalTokenTree = crate::TokenTree;
+type ExternalTokenStream = crate::TokenStream;
 
 // TODO: There surely is a better way to achieve this. I don't like this
 // "duplication" of the TokenTree enumeration. But I cannot use the public
@@ -13,8 +17,21 @@ pub enum TokenTree {
     Literal(Literal),
 }
 
+impl From<ExternalTokenTree> for TokenTree {
+    fn from(value: ExternalTokenTree) -> Self {
+        match value {
+            ExternalTokenTree::Group(g) => TokenTree::Group(g.0),
+            ExternalTokenTree::Ident(i) => TokenTree::Ident(i.0),
+            ExternalTokenTree::Punct(p) => TokenTree::Punct(p.0),
+            ExternalTokenTree::Literal(l) => TokenTree::Literal(l.0),
+        }
+    }
+}
+
 extern "C" {
     fn TokenStream__new() -> TokenStream;
+    fn TokenStream__with_capacity(capacity: u64) -> TokenStream;
+    fn TokenStream__push(stream: *mut TokenStream, tree: TokenTree);
 }
 
 #[repr(C)]
@@ -22,6 +39,7 @@ extern "C" {
 pub struct TokenStream {
     pub(crate) data: *const TokenTree,
     pub(crate) size: u64,
+    capacity: u64,
 }
 
 impl TokenStream {
@@ -29,7 +47,41 @@ impl TokenStream {
         unsafe { TokenStream__new() }
     }
 
+    fn with_capacity(capacity: u64) -> Self {
+        unsafe { TokenStream__with_capacity(capacity) }
+    }
+
+    fn push(&mut self, tree: TokenTree) {
+        unsafe { TokenStream__push(self as *mut TokenStream, tree) }
+    }
+
     pub fn is_empty(&self) -> bool {
         0 == self.size
     }
+
+    pub fn from_iterator<I>(it: I) -> Self
+    where
+        I: IntoIterator<Item = ExternalTokenStream>,
+    {
+        let it = it.into_iter();
+        let mut result = TokenStream::with_capacity(it.size_hint().0.try_into().unwrap());
+        for stream in it {
+            for item in stream.into_iter() {
+                result.push(item.into());
+            }
+        }
+        result
+    }
+
+    pub fn from_tree_iterator<I>(it: I) -> Self
+    where
+        I: IntoIterator<Item = ExternalTokenTree>,
+    {
+        let it = it.into_iter();
+        let mut result = TokenStream::with_capacity(it.size_hint().0.try_into().unwrap());
+        for item in it {
+            result.push(item.into());
+        }
+        result
+    }
 }
diff --git a/librust/proc_macro/rust/lib.rs b/librust/proc_macro/rust/lib.rs
index 31aa6ea7c75..4888edb16a8 100644
--- a/librust/proc_macro/rust/lib.rs
+++ b/librust/proc_macro/rust/lib.rs
@@ -174,14 +174,14 @@ impl FromStr for TokenStream {
 }
 
 impl iter::FromIterator<TokenTree> for TokenStream {
-    fn from_iter<I: IntoIterator<Item = TokenTree>>(_trees: I) -> Self {
-        todo!("Implement this function")
+    fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
+        TokenStream(bridge::token_stream::TokenStream::from_tree_iterator(trees))
     }
 }
 
 impl iter::FromIterator<TokenStream> for TokenStream {
-    fn from_iter<I: IntoIterator<Item = TokenStream>>(_streams: I) -> Self {
-        todo!("Implement this function")
+    fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
+        TokenStream(bridge::token_stream::TokenStream::from_iterator(streams))
     }
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-06 21:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:31 [gcc/devel/rust/master] libproc_macro: Implement FromIterator Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).