MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / tyid

Method tyid

crates/wit-bindgen/src/rust.rs:79–199  ·  view source on GitHub ↗
(&self, id: TypeId, mode: TypeMode)

Source from the content-addressed store, hash-verified

77 }
78
79 fn tyid(&self, id: TypeId, mode: TypeMode) -> String {
80 let info = self.info(id);
81 let lt = self.lifetime_for(&info, mode);
82 let ty = &self.resolve().types[id];
83 if ty.name.is_some() {
84 // If this type has a list internally, no lifetime is being printed,
85 // but we're in a borrowed mode, then that means we're in a borrowed
86 // context and don't want ownership of the type but we're using an
87 // owned type definition. Inject a `&` in front to indicate that, at
88 // the API level, ownership isn't required.
89 let mut out = String::new();
90 if info.has_list && lt.is_none() {
91 if let TypeMode::AllBorrowed(lt) = mode {
92 if lt != "'_" {
93 out.push_str(&format!("&{lt} "))
94 } else {
95 out.push_str("&")
96 }
97 }
98 }
99 let name = if lt.is_some() {
100 self.param_name(id)
101 } else {
102 self.result_name(id)
103 };
104 out.push_str(&self.type_name_in_interface(ty.owner, &name));
105
106 // If the type recursively owns data and it's a
107 // variant/record/list, then we need to place the
108 // lifetime parameter on the type as well.
109 if info.has_list && needs_generics(self.resolve(), &ty.kind) {
110 out.push_str(&self.generics(lt));
111 }
112
113 return out;
114
115 fn needs_generics(resolve: &Resolve, ty: &TypeDefKind) -> bool {
116 match ty {
117 TypeDefKind::Variant(_)
118 | TypeDefKind::Record(_)
119 | TypeDefKind::Option(_)
120 | TypeDefKind::Result(_)
121 | TypeDefKind::Future(_)
122 | TypeDefKind::Stream(_)
123 | TypeDefKind::List(_)
124 | TypeDefKind::Map(_, _)
125 | TypeDefKind::Flags(_)
126 | TypeDefKind::Enum(_)
127 | TypeDefKind::Tuple(_)
128 | TypeDefKind::Handle(_)
129 | TypeDefKind::Resource => true,
130 TypeDefKind::Type(Type::Id(t)) => {
131 needs_generics(resolve, &resolve.types[*t].kind)
132 }
133 TypeDefKind::Type(Type::String) => true,
134 TypeDefKind::Type(_) => false,
135 TypeDefKind::Unknown => unreachable!(),
136 TypeDefKind::FixedLengthList(..) => todo!(),

Callers 1

tyMethod · 0.80

Implementers 1

lib.rscrates/wit-bindgen/src/lib.rs

Calls 15

infoMethod · 0.80
lifetime_forMethod · 0.80
param_nameMethod · 0.80
result_nameMethod · 0.80
genericsMethod · 0.80
optional_tyMethod · 0.80
wasmtime_pathMethod · 0.80
newFunction · 0.50
resolveMethod · 0.45
is_someMethod · 0.45
is_noneMethod · 0.45

Tested by

no test coverage detected