MCPcopy Create free account
hub / github.com/battlecode/battlecode-2018 / CEnumWrapper

Class CEnumWrapper

bindings/frankenswig/enums.py:85–162  ·  view source on GitHub ↗

A wrapper for a rust c-style enum, that is, an enum with integer values.

Source from the content-addressed store, hash-verified

83 return f'{"&" if self.is_ref else ""}{self.wrapper.module}::{self.wrapper.name}'
84
85class CEnumWrapper(CEnum, DeriveMixins):
86 '''A wrapper for a rust c-style enum, that is, an enum with integer values.'''
87
88 def __init__(self, program, name, docs=''):
89 self.program = program
90 self.name = name
91 self.type = CEnumWrapperType(self)
92 super().__init__(program.module, self.type.san_name)
93 self.module = program.module
94 self.docs = docs
95 self.methods = []
96
97 def variant(self, name, value, docs=''):
98 # TODO: docs
99 super().variant(name, value)
100 return self
101
102 def method(self, type, name, args, docs='', pyname=None, self_ref=False):
103 original = f'{self.type.orig_name}::{name}'
104 if self_ref:
105 actual_args = [Var(self.type.mut_ref(), 'this')] + args
106 else:
107 actual_args = [Var(self.type, 'this')] + args
108
109 if pyname is None:
110 pyname = name
111
112 self.methods.append(Method(type, self.c_name, name, actual_args,
113 make_safe_call(type, original, actual_args), docs=docs
114 , pyname=pyname))
115
116 return self
117
118 def to_rust(self):
119 decl = super().to_rust()
120
121 for t1, t2 in [(self.type.c_name, self.type.orig_name), (self.type.orig_name, self.type.c_name)]:
122 start = s(f'''\
123 impl Into<{t2}> for {t1} {{
124 fn into(self) -> {t2} {{
125 match self {{
126 ''&#x27;)
127
128 body = ''
129 for variant,_ in self.variants:
130 body += f'{t1}::{variant} => {t2}::{variant},\n'
131 body += f'_ => {t2}::{self.variants[0][0]},\n'
132
133
134 end = s(f''&#x27;\
135 }}
136 }}
137 }}
138 ''&#x27;)
139
140 decl += start + s(body, indent=12) + end
141
142 methods = '\n'.join(m.to_rust() for m in self.methods)

Callers 1

c_enumMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected