Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Commit d4084fb

Browse files
authored
impl From<T> for ast::Ast (RustPython#62)
1 parent 33a3c40 commit d4084fb

File tree

2 files changed

+628
-0
lines changed

2 files changed

+628
-0
lines changed

ast/asdl_rs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,16 @@ def visitModule(self, mod):
322322
const FIELD_NAMES: &'static [&'static str] = &[];
323323
}
324324
""", 0)
325+
for dfn in mod.dfns:
326+
rust_name = rust_type_name(dfn.name)
327+
generics = "" if self.type_info[dfn.name].is_simple else "<R>"
328+
self.emit(f"""
329+
impl<R> From<{rust_name}{generics}> for Ast<R> {{
330+
fn from(node: {rust_name}{generics}) -> Self {{
331+
Ast::{rust_name}(node)
332+
}}
333+
}}
334+
""", 0)
325335

326336
for dfn in mod.dfns:
327337
self.visit(dfn)
@@ -385,6 +395,11 @@ def simple_sum(self, sum, type, depth):
385395
{rust_name}::{cons.name}
386396
}}
387397
}}
398+
impl<R> From<{rust_name}{cons.name}> for Ast<R> {{
399+
fn from(_: {rust_name}{cons.name}) -> Self {{
400+
{rust_name}::{cons.name}.into()
401+
}}
402+
}}
388403
impl Node for {rust_name}{cons.name} {{
389404
const NAME: &'static str = "{cons.name}";
390405
const FIELD_NAMES: &'static [&'static str] = &[];
@@ -447,6 +462,11 @@ def sum_subtype_struct(self, sum_type_info, t, rust_name, depth):
447462
{rust_name}::{t.name}(payload)
448463
}}
449464
}}
465+
impl<R> From<{payload_name}<R>> for Ast<R> {{
466+
fn from(payload: {payload_name}<R>) -> Self {{
467+
{rust_name}::from(payload).into()
468+
}}
469+
}}
450470
""",
451471
depth,
452472
)

0 commit comments

Comments
 (0)