MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / print_rust_enum

Method print_rust_enum

crates/rust/src/interface.rs:2143–2232  ·  view source on GitHub ↗
(
        &mut self,
        id: TypeId,
        cases: impl IntoIterator<Item = (String, &'b Docs, Option<&'b Type>)> + Clone,
        docs: &Docs,
    )

Source from the content-addressed store, hash-verified

2141 }
2142
2143 fn print_rust_enum<'b>(
2144 &mut self,
2145 id: TypeId,
2146 cases: impl IntoIterator<Item = (String, &'b Docs, Option<&'b Type>)> + Clone,
2147 docs: &Docs,
2148 ) where
2149 Self: Sized,
2150 {
2151 let info = self.info(id);
2152 // We use a BTree set to make sure we don't have any duplicates and have a stable order
2153 let additional_derives: BTreeSet<String> = self
2154 .r#gen
2155 .opts
2156 .additional_derive_attributes
2157 .iter()
2158 .cloned()
2159 .collect();
2160 for (name, mode) in self.modes_of(id) {
2161 self.rustdoc(docs);
2162 let mut derives = BTreeSet::new();
2163 if !self
2164 .r#gen
2165 .opts
2166 .additional_derive_ignore
2167 .contains(&name.to_kebab_case())
2168 {
2169 derives.extend(additional_derives.clone());
2170 }
2171 if info.is_copy() {
2172 derives.extend(["Copy", "Clone"].into_iter().map(|s| s.to_string()));
2173 } else if info.is_clone() {
2174 derives.insert("Clone".to_string());
2175 }
2176 if !derives.is_empty() {
2177 self.push_str("#[derive(");
2178 self.push_str(&derives.into_iter().collect::<Vec<_>>().join(", "));
2179 self.push_str(")]\n")
2180 }
2181 self.push_str(&format!("pub enum {name}"));
2182 self.print_generics(mode.lifetime);
2183 self.push_str(" {\n");
2184 for (case_name, docs, payload) in cases.clone() {
2185 self.rustdoc(docs);
2186 self.push_str(&case_name);
2187 if let Some(ty) = payload {
2188 self.push_str("(");
2189 let mode = self.filter_mode(ty, mode);
2190 self.print_ty(ty, mode);
2191 self.push_str(")")
2192 }
2193 self.push_str(",\n");
2194 }
2195 self.push_str("}\n");
2196
2197 self.print_rust_enum_debug(
2198 mode,
2199 &name,
2200 cases

Callers 1

print_typedef_variantMethod · 0.80

Calls 15

newFunction · 0.85
infoMethod · 0.80
collectMethod · 0.80
modes_ofMethod · 0.80
rustdocMethod · 0.80
extendMethod · 0.80
is_copyMethod · 0.80
mapMethod · 0.80
is_cloneMethod · 0.80
joinMethod · 0.80
print_genericsMethod · 0.80
filter_modeMethod · 0.80

Tested by

no test coverage detected