Struct ExternModuleTranslator

Source
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§

§shared_enums: HashSet<ItemEnum>§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

Source

pub fn new(shared_enums: HashSet<ItemEnum>) -> Self

Source

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.

Source

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.

Source

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.

Source

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.");
}
Source

pub fn get_enum(&self, ident_str: &str) -> Option<&ItemEnum>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.