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

Method modes_of

crates/rust/src/interface.rs:2003–2037  ·  view source on GitHub ↗
(&self, ty: TypeId)

Source from the content-addressed store, hash-verified

2001 }
2002
2003 fn modes_of(&self, ty: TypeId) -> Vec<(String, TypeMode)> {
2004 let info = self.info(ty);
2005 let mut result = Vec::new();
2006 if !self.r#gen.opts.generate_unused_types {
2007 // If this type isn't actually used, no need to generate it.
2008 if !info.owned && !info.borrowed {
2009 return result;
2010 }
2011 }
2012 // Generate one mode for when the type is owned and another for when
2013 // it's borrowed.
2014 let a = self.type_mode_for_id(ty, TypeOwnershipStyle::Owned, "'a");
2015 let b = self.type_mode_for_id(ty, TypeOwnershipStyle::Borrowed, "'a");
2016
2017 if self.uses_two_names(&info) {
2018 // If this type uses two names then, well, it uses two names. In
2019 // this situation both modes are returned.
2020 assert!(a != b);
2021 result.push((self.result_name(ty), a));
2022 result.push((self.param_name(ty), b));
2023 } else if a == b {
2024 // If the modes are the same then there's only one result.
2025 result.push((self.result_name(ty), a));
2026 } else if info.owned || matches!(self.r#gen.opts.ownership, Ownership::Owning) {
2027 // If this type is owned or if ownership is preferred then the owned
2028 // variant is used as a priority. This is where the generator's
2029 // configuration comes into play.
2030 result.push((self.result_name(ty), a));
2031 } else {
2032 // And finally, failing all that, the borrowed variant is used.
2033 assert!(!info.owned);
2034 result.push((self.param_name(ty), b));
2035 }
2036 result
2037 }
2038
2039 fn print_typedef_record(&mut self, id: TypeId, record: &Record, docs: &Docs) {
2040 let info = self.info(id);

Callers 9

print_typedef_recordMethod · 0.80
print_rust_enumMethod · 0.80
print_typedef_optionMethod · 0.80
print_typedef_resultMethod · 0.80
print_typedef_aliasMethod · 0.80
type_tupleMethod · 0.80
type_listMethod · 0.80
type_mapMethod · 0.80

Calls 7

newFunction · 0.85
infoMethod · 0.80
type_mode_for_idMethod · 0.80
uses_two_namesMethod · 0.80
result_nameMethod · 0.80
param_nameMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected