Skip to content

Commit 7da8279

Browse files
authored
Merge pull request #2 from chavic/chavic/primitives-gen
Chavic/primitives gen
2 parents 505f940 + 5eff6ef commit 7da8279

File tree

5 files changed

+133
-15
lines changed

5 files changed

+133
-15
lines changed

fixtures/arithmetic/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ pub fn multiply(left: u32, right: u32) -> u32 {
1010
left * right
1111
}
1212

13-
#[uniffi::export]
14-
pub fn devide(left: u32, right: u32) -> Option<u32> {
15-
left.checked_div(right)
16-
}
13+
// #[uniffi::export]
14+
// pub fn devide(left: u32, right: u32) -> Option<u32> {
15+
// left.checked_div(right)
16+
// }
1717

1818
#[uniffi::export]
1919
pub fn add_u8(left: u8, right: u8) -> u8 {

fixtures/arithmetic/test/simple_arithmetic_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ void main() {
1010
expect(api.multiply(2, 8), 16);
1111
});
1212
test('u8', () {
13-
expect(api.add_u8(2, 2), 4);
13+
expect(api.addU8(2, 2), 4);
1414
});
1515
test('u16', () {
16-
expect(api.add_u16(2, 2), 4);
16+
expect(api.addU16(2, 2), 4);
1717
});
1818
test('u64', () {
19-
expect(api.add_u64(2, 2), 4);
19+
expect(api.addU64(2, 2), 4);
2020
});
2121

2222
test('i8', () {
23-
expect(api.add_i8(2, 2), 4);
23+
expect(api.addI8(2, 2), 4);
2424
});
2525
test('i16', () {
26-
expect(api.add_i16(2, 2), 4);
26+
expect(api.addI16(2, 2), 4);
2727
});
2828
test('i32', () {
29-
expect(api.add_i32(2, 2), 4);
29+
expect(api.addI32(2, 2), 4);
3030
});
3131
test('i64', () {
32-
expect(api.add_i64(2, 2), 4);
32+
expect(api.addI64(2, 2), 4);
3333
});
3434
}

src/gen.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use uniffi_bindgen::{BindingGenerator, BindingGeneratorConfig, ComponentInterfac
1111
mod enums;
1212
mod functions;
1313
mod objects;
14+
mod primitives;
1415
mod records;
1516
mod types;
1617
mod utils;

src/gen/primitives.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use paste::paste;
2+
use uniffi_bindgen::backend::{CodeOracle, CodeType, Literal};
3+
use uniffi_bindgen::interface::{types::Type, Radix};
4+
5+
fn render_literal(_oracle: &dyn CodeOracle, literal: &Literal) -> String {
6+
fn typed_number(type_: &Type, num_str: String) -> String {
7+
match type_ {
8+
Type::Int8
9+
| Type::UInt8
10+
| Type::Int16
11+
| Type::UInt16
12+
| Type::Int32
13+
| Type::UInt32
14+
| Type::UInt64
15+
| Type::Float32
16+
| Type::Float64 => num_str,
17+
_ => panic!("Unexpected literal: {} is not a number", num_str),
18+
}
19+
}
20+
21+
match literal {
22+
Literal::Boolean(v) => format!("{}", v),
23+
Literal::String(s) => format!("\"{}\"", s),
24+
Literal::Int(i, radix, type_) => typed_number(
25+
type_,
26+
match radix {
27+
Radix::Octal => format!("{:#x}", i),
28+
Radix::Decimal => format!("{}", i),
29+
Radix::Hexadecimal => format!("{:#x}", i),
30+
},
31+
),
32+
Literal::UInt(i, radix, type_) => typed_number(
33+
type_,
34+
match radix {
35+
Radix::Octal => format!("{:#x}", i),
36+
Radix::Decimal => format!("{}", i),
37+
Radix::Hexadecimal => format!("{:#x}", i),
38+
},
39+
),
40+
Literal::Float(string, type_) => typed_number(type_, string.clone()),
41+
42+
_ => unreachable!("Literal"),
43+
}
44+
}
45+
46+
macro_rules! impl_code_type_for_primitive {
47+
($T:ty, $class_name:literal) => {
48+
paste! {
49+
#[derive(Debug)]
50+
pub struct $T;
51+
52+
impl CodeType for $T {
53+
fn type_label(&self, _oracle: &dyn CodeOracle) -> String {
54+
$class_name.into()
55+
}
56+
57+
fn literal(&self, oracle: &dyn CodeOracle, literal: &Literal) -> String {
58+
render_literal(oracle, &literal)
59+
}
60+
}
61+
}
62+
};
63+
}
64+
65+
impl_code_type_for_primitive!(BooleanCodeType, "bool");
66+
impl_code_type_for_primitive!(StringCodeType, "String");
67+
impl_code_type_for_primitive!(BytesCodeType, "Uint8List");
68+
impl_code_type_for_primitive!(Int8CodeType, "int");
69+
impl_code_type_for_primitive!(Int16CodeType, "int");
70+
impl_code_type_for_primitive!(Int32CodeType, "int");
71+
impl_code_type_for_primitive!(Int64CodeType, "int");
72+
impl_code_type_for_primitive!(UInt8CodeType, "int");
73+
impl_code_type_for_primitive!(UInt16CodeType, "int");
74+
impl_code_type_for_primitive!(UInt32CodeType, "int");
75+
impl_code_type_for_primitive!(UInt64CodeType, "int");
76+
impl_code_type_for_primitive!(Float32CodeType, "double");
77+
impl_code_type_for_primitive!(Float64CodeType, "double");

src/gen/types.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ pub fn generate_ffi_type(ret: Option<&FfiType>) -> dart::Tokens {
66
return quote!(Void)
77
};
88
match *ret_type {
9+
FfiType::UInt8 => quote!(Uint8),
10+
FfiType::UInt16 => quote!(Uint16),
911
FfiType::UInt32 => quote!(Uint32),
10-
FfiType::Int8 => quote!(Uint8),
12+
FfiType::UInt64 => quote!(Uint64),
13+
FfiType::Int8 => quote!(Int8),
14+
FfiType::Int16 => quote!(Int16),
15+
FfiType::Int32 => quote!(Int32),
16+
FfiType::Int64 => quote!(Int64),
1117
FfiType::RustBuffer(ref inner) => match inner {
1218
Some(i) => quote!($i),
1319
_ => quote!(RustBuffer),
@@ -22,8 +28,14 @@ pub fn generate_ffi_dart_type(ret: Option<&FfiType>) -> dart::Tokens {
2228
return quote!(void)
2329
};
2430
match *ret_type {
31+
FfiType::UInt8 => quote!(int),
32+
FfiType::UInt16 => quote!(int),
2533
FfiType::UInt32 => quote!(int),
34+
FfiType::UInt64 => quote!(int),
2635
FfiType::Int8 => quote!(int),
36+
FfiType::Int16 => quote!(int),
37+
FfiType::Int32 => quote!(int),
38+
FfiType::Int64 => quote!(int),
2739
FfiType::RustBuffer(ref inner) => match inner {
2840
Some(i) => quote!($i),
2941
_ => quote!(RustBuffer),
@@ -35,7 +47,16 @@ pub fn generate_ffi_dart_type(ret: Option<&FfiType>) -> dart::Tokens {
3547

3648
pub fn generate_type(ty: &Type) -> dart::Tokens {
3749
match ty {
38-
Type::UInt8 | Type::UInt32 => quote!(int),
50+
Type::UInt8
51+
| Type::UInt32
52+
| Type::Int8
53+
| Type::Int16
54+
| Type::Int64
55+
| Type::UInt16
56+
| Type::Int32
57+
| Type::UInt64
58+
| Type::Float32
59+
| Type::Float64 => quote!(int),
3960
Type::String => quote!(String),
4061
Type::Object(name) => quote!($name),
4162
Type::Boolean => quote!(bool),
@@ -87,7 +108,16 @@ pub fn convert_to_rust_buffer(ty: &Type, inner: dart::Tokens) -> dart::Tokens {
87108

88109
pub fn type_lift_fn(ty: &Type, inner: dart::Tokens) -> dart::Tokens {
89110
match ty {
90-
Type::UInt32 => inner,
111+
Type::Int8
112+
| Type::UInt8
113+
| Type::Int16
114+
| Type::UInt16
115+
| Type::Int32
116+
| Type::Int64
117+
| Type::UInt32
118+
| Type::UInt64
119+
| Type::Float32
120+
| Type::Float64 => inner,
91121
Type::Boolean => quote!(($inner) > 0),
92122
Type::String => quote!(liftString(api, $inner)),
93123
Type::Object(name) => quote!($name.lift(api, $inner)),
@@ -100,7 +130,17 @@ pub fn type_lift_fn(ty: &Type, inner: dart::Tokens) -> dart::Tokens {
100130

101131
pub fn type_lower_fn(ty: &Type, inner: dart::Tokens) -> dart::Tokens {
102132
match ty {
103-
Type::UInt32 | Type::Boolean => inner,
133+
Type::UInt32
134+
| Type::Int8
135+
| Type::UInt8
136+
| Type::Int16
137+
| Type::UInt16
138+
| Type::Int32
139+
| Type::Int64
140+
| Type::UInt64
141+
| Type::Float32
142+
| Type::Float64
143+
| Type::Boolean => inner,
104144
Type::String => quote!(lowerString(api, $inner)),
105145
Type::Object(name) => quote!($name.lower(api, $inner)),
106146
Type::Optional(o) => {

0 commit comments

Comments
 (0)