rusty_bind_parser/
trait_function_helpers.rs

1use crate::extern_module_translator::{
2    Arg,
3    Function,
4    ReferenceParameters,
5    RustWrapperType,
6    WrapperType,
7};
8
9// Helper function for returning primitives
10fn primitive(type_: &str) -> Option<WrapperType> {
11    Some(WrapperType {
12        original_type_name: syn::parse_str(type_).unwrap(),
13        wrapper_name: type_.to_string(),
14        rust_type: RustWrapperType::Primitive,
15        reference_parameters: None,
16        impl_traits: vec![],
17    })
18}
19
20// Here are all builtin trait function implementations supported by rusty-bind
21pub fn get_trait_functions(type_: String, trait_: String) -> Option<Vec<Function>> {
22    // Returning "None" means the trait is not supported (yet)
23    // Returning Some(vec![]) means the trait is a marker (no methods)
24    // Returning Some(vec![<trait functions>]) means the trait has methods
25    match trait_.as_str() {
26        "Eq" | "PartialEq" => Some(
27            ["eq", "ne"]
28                .iter()
29                .map(|ident| Function {
30                    name: ident.to_string(),
31                    arguments: vec![
32                        Arg::_self(type_.as_str()),
33                        Arg::_default("other", type_.as_str()),
34                    ],
35                    return_type: primitive("bool"),
36                })
37                .collect(),
38        ),
39        "Hash" => Some(vec![Function {
40            name: "hash".to_owned(),
41            arguments: vec![
42                Arg::_self(type_.as_str()),
43                Arg::new(
44                    "state",
45                    "dyn Hasher",
46                    Some(ReferenceParameters {
47                        is_static: true,
48                        is_mut: true,
49                        boxed: true,
50                    }),
51                ),
52            ],
53            return_type: None,
54        }]),
55        "PartialOrd" | "Ord" => Some(vec![
56            Function {
57                name: "gt".to_owned(),
58                arguments: vec![
59                    Arg::_self(type_.as_str()),
60                    Arg::_default("other", type_.as_str()),
61                ],
62                return_type: primitive("bool"),
63            },
64            Function {
65                name: "ge".to_owned(),
66                arguments: vec![
67                    Arg::_self(type_.as_str()),
68                    Arg::_default("other", type_.as_str()),
69                ],
70                return_type: primitive("bool"),
71            },
72            Function {
73                name: "lt".to_owned(),
74                arguments: vec![
75                    Arg::_self(type_.as_str()),
76                    Arg::_default("other", type_.as_str()),
77                ],
78                return_type: primitive("bool"),
79            },
80            Function {
81                name: "le".to_owned(),
82                arguments: vec![
83                    Arg::_self(type_.as_str()),
84                    Arg::_default("other", type_.as_str()),
85                ],
86                return_type: primitive("bool"),
87            },
88        ]),
89        // Read exposes custom trait FFIRead with custom method `fn ffi_read(&mut self, arr_ptr: &mut u8, arr_len: usize) -> usize`
90        "Read" => Some(vec![Function {
91            name: "ffi_read".to_owned(),
92            arguments: vec![
93                Arg::_mut_self(type_.as_str()),
94                Arg::new(
95                    "buf_ptr",
96                    "u8",
97                    Some(ReferenceParameters {
98                        is_static: false,
99                        is_mut: true,
100                        boxed: false,
101                    }),
102                ),
103                Arg::primitive("buf_len", "usize", None),
104            ],
105            return_type: primitive("usize"),
106        }]),
107        // Some languages (Swift) have no interface/protocol/template for Write
108        "Write" => Some(vec![
109            Function {
110                name: "ffi_write".to_owned(),
111                arguments: vec![
112                    Arg::_mut_self(type_.as_str()),
113                    Arg::new(
114                        "buf_ptr",
115                        "u8",
116                        Some(ReferenceParameters {
117                            is_static: false,
118                            is_mut: false,
119                            boxed: false,
120                        }),
121                    ),
122                    Arg::primitive("buf_len", "usize", None),
123                ],
124                // Not `Result<usize>` for easier usage
125                return_type: primitive("usize"),
126            },
127            Function {
128                name: "ffi_flush".to_owned(),
129                arguments: vec![Arg::_mut_self(type_.as_str())],
130                // Not `Result<()>` for easier usage
131                return_type: primitive("bool"),
132            },
133        ]),
134        "Sync" | "Send" => Some(vec![]),
135        _ => None,
136    }
137}