pub struct ExternModuleTranslator {
pub shared_enums: HashSet<ItemEnum>,
pub exception_names: HashSet<String>,
pub rust_types_wrappers: OrderedHashSet<WrapperType>,
pub user_custom_types: HashMap<WrapperType, Vec<Function>>,
pub user_traits: HashMap<WrapperType, Vec<Function>>,
pub global_functions: Vec<Function>,
pub exception_trait_methods: HashSet<Function>,
pub declared_impls: HashMap<String, Vec<String>>,
}
Expand description
The structure keeps the Rust types wrappers,
methods of the structures and global functions
signatures. user_custom_types
is a subset
of rust_types_wrappers
, since the latter collection
keeps all the wrappers that will be handled, and the
first one keeps only a user defined types wrappers
along with their methods.
Fields§
§exception_names: HashSet<String>
§rust_types_wrappers: OrderedHashSet<WrapperType>
§user_custom_types: HashMap<WrapperType, Vec<Function>>
§user_traits: HashMap<WrapperType, Vec<Function>>
§global_functions: Vec<Function>
§exception_trait_methods: HashSet<Function>
§declared_impls: HashMap<String, Vec<String>>
Implementations§
Source§impl ExternModuleTranslator
impl ExternModuleTranslator
pub fn new(shared_enums: HashSet<ItemEnum>) -> Self
Sourcepub fn parse_items_in_extern_rust_module(
&mut self,
extern_module: &ItemForeignMod,
context: &BuildContext,
) -> Result<()>
pub fn parse_items_in_extern_rust_module( &mut self, extern_module: &ItemForeignMod, context: &BuildContext, ) -> Result<()>
Takes extern “Rust” module and translates its functions. It also stores information about the functions signatures for a further processing.
Sourcepub fn translate_trait_external_modules(
&mut self,
extern_module: &ItemForeignMod,
context: &BuildContext,
) -> Result<()>
pub fn translate_trait_external_modules( &mut self, extern_module: &ItemForeignMod, context: &BuildContext, ) -> Result<()>
Takes extern “Traits” module and translates its functions. It also stores information about the functions signatures for a further processing.
Sourcepub fn translate_exception_trait_external_module(
&mut self,
extern_module: &ItemForeignMod,
context: &BuildContext,
) -> Result<()>
pub fn translate_exception_trait_external_module( &mut self, extern_module: &ItemForeignMod, context: &BuildContext, ) -> Result<()>
Takes extern “ExceptionTrait” module and translates its methods. It also stores information about the functions signatures for a further processing.
Sourcepub fn translate_function(
&mut self,
function: &ForeignItemFn,
) -> Result<(Option<WrapperType>, Function)>
pub fn translate_function( &mut self, function: &ForeignItemFn, ) -> Result<(Option<WrapperType>, Function)>
Example:
fn foo(self: &SomeType) -> Result<AnotherType>
==>
fn foo(self: &SomeType) -> ResultAnotherType
.
The method takes the Rust function signature and
registers every type as a WrapperType
as well as
translates it into inner function form Function
.
If the type has a self
argument it returns its
WrapperType in the result.
Example:
use syn::{ForeignItemFn, parse_quote};
use rusty_bind_parser::extern_module_translator::*;
let mut local_module_translator = ExternModuleTranslator::new(Default::default());
let function: ForeignItemFn = parse_quote! { fn foo(self: &SomeType, a: u32) -> Option<u32>; };
if let (Some(associated_type), Function {
arguments,
return_type: Some(return_type),
name,
}) = local_module_translator.translate_function(&function).unwrap() {
assert!(name == "foo");
assert!(arguments.len() == 2);
assert!(arguments[0] == Arg {arg_name: "self".to_owned(), typ: WrapperType {
original_type_name: parse_quote! { SomeType },
wrapper_name: "SomeType".to_owned(),
rust_type: RustWrapperType::Custom,
reference_parameters: Some(ReferenceParameters::shared()),
impl_traits: vec![]
}});
assert!(arguments[1] == Arg {arg_name: "a".to_owned(), typ: WrapperType {
original_type_name: parse_quote! { u32 },
wrapper_name: "u32".to_owned(),
rust_type: RustWrapperType::Primitive,
reference_parameters: None,
impl_traits: vec![]
}});
assert!(associated_type == WrapperType {
original_type_name: parse_quote! { SomeType },
wrapper_name: "SomeType".to_owned(),
rust_type: RustWrapperType::Custom ,
reference_parameters: Some(ReferenceParameters::shared()),
impl_traits: vec![]
});
println!("{return_type:?}");
assert!(return_type == WrapperType {
original_type_name: parse_quote! { Option<u32> },
wrapper_name: "Optionalu32".to_owned(),
rust_type: RustWrapperType::Option(
WrapperType {
original_type_name: parse_quote! { u32 },
wrapper_name: "u32".to_owned(),
rust_type: RustWrapperType::Primitive,
reference_parameters: None,
impl_traits: vec![]
}.boxed()
),
reference_parameters: None,
impl_traits: vec![]
});
} else {
panic!("Translated function doesn't match.");
}