GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — flatbuffers-python/codegen/unions.rs at master · RLBot/flatbuffers-python

Expand file tree

/

unions.rs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
179 lines (146 loc) · 4.88 KB
/

unions.rs

Copy path

File metadata and controls

179 lines (146 loc) · 4.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use indexmap::IndexMap;
use planus_types::intermediate::UnionVariant;
use std::borrow::Cow;
macro_rules! write_str {
($self:ident, $s:expr) => {
$self.file_contents.push(Cow::Borrowed($s))
};
}
macro_rules! write_fmt {
($self:ident, $($arg:tt)*) => {
$self.file_contents.push(Cow::Owned(format!($($arg)*)))
};
}
pub struct UnionBindGenerator<'a> {
name: &'a str,
variants: &'a IndexMap<String, UnionVariant>,
file_contents: Vec<Cow<'static, str>>,
}
impl<'a> UnionBindGenerator<'a> {
pub fn new(name: &'a str, variants: &'a IndexMap<String, UnionVariant>) -> Self {
Self {
name,
variants,
file_contents: Vec::new(),
}
}
fn generate_definition(&mut self) {
write_fmt!(self, "#[derive(pyo3::FromPyObject)]");
write_fmt!(self, "pub enum {} {{", self.name);
for var_name in self.variants.keys() {
write_fmt!(self, " {var_name}(Py<super::{var_name}>),");
}
write_str!(self, "}");
write_str!(self, "");
write_fmt!(self, "impl {} {{", self.name);
write_str!(self, " pub fn py_default(py: Python) -> Py<PyAny> {");
let first_var_name = self.variants.keys().next().unwrap();
write_fmt!(
self,
" super::{first_var_name}::py_default(py).into_any()",
);
write_str!(self, " }");
write_str!(self, "}");
write_str!(self, "");
}
fn generate_from_flat_impls(&mut self) {
write_fmt!(
self,
"impl FromGil<&flat::{}> for {} {{",
self.name,
self.name
);
write_fmt!(
self,
" fn from_gil(py: Python, flat_t: &flat::{}) -> Self {{",
self.name
);
write_str!(self, " match flat_t {");
for var_name in self.variants.keys() {
write_fmt!(
self,
" flat::{}::{var_name}(item) => ",
self.name,
);
write_fmt!(self, " Self::{var_name}(");
write_fmt!(
self,
" Py::new(py, super::{var_name}::from_gil(py, &**item)).unwrap(),"
);
write_str!(self, " ),");
}
write_str!(self, " }");
write_str!(self, " }");
write_str!(self, "}");
write_str!(self, "");
}
fn generate_to_flat_impls(&mut self) {
write_fmt!(
self,
"impl FromGil<&{}> for flat::{} {{",
self.name,
self.name
);
write_fmt!(
self,
" fn from_gil(py: Python, py_type: &{}) -> Self {{",
self.name
);
write_str!(self, " match py_type {");
for var_name in self.variants.keys() {
write_fmt!(self, " {}::{var_name}(item) => {{", self.name,);
write_fmt!(
self,
" flat::{}::{var_name}(Box::new(crate::from_py_into(py, item)))",
self.name
);
write_str!(self, " },");
}
write_str!(self, " }");
write_str!(self, " }");
write_str!(self, "}");
write_str!(self, "");
}
fn generate_into_pyany_method(&mut self) {
write_str!(self, " pub fn into_any(self) -> Py<PyAny> {");
write_str!(self, " match self {");
for var_name in self.variants.keys() {
write_fmt!(
self,
" Self::{var_name}(item) => item.into_any(),"
);
}
write_str!(self, " }");
write_str!(self, " }");
}
fn generate_repr_method(&mut self) {
write_str!(self, " pub fn __repr__(&self, py: Python) -> String {");
write_str!(self, " match self {");
for var_name in self.variants.keys() {
write_fmt!(
self,
" Self::{var_name}(item) => item.borrow(py).__repr__(py),"
);
}
write_str!(self, " }");
write_str!(self, " }");
}
fn generate_py_methods(&mut self) {
write_fmt!(self, "impl {} {{", self.name);
self.generate_into_pyany_method();
write_str!(self, "");
self.generate_repr_method();
write_str!(self, "}");
write_str!(self, "");
}
pub fn generate_binds(mut self) -> Vec<Cow<'static, str>> {
write_str!(self, "use crate::{FromGil, PyDefault, flat};");
write_str!(self, "use pyo3::prelude::*;");
write_str!(self, "");
self.generate_definition();
self.generate_from_flat_impls();
self.generate_to_flat_impls();
self.generate_py_methods();
self.file_contents
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.