-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from acterglobal/issue26_duration-type-support
Add support for duration primitive type in code generation
- Loading branch information
Showing
11 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "duration_type_test" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
|
||
[lib] | ||
name = "duration_type_test" | ||
crate-type = ["lib", "cdylib"] | ||
|
||
[dependencies] | ||
uniffi = { workspace = true } | ||
|
||
[build-dependencies] | ||
uniffi-dart = { path = "../../", features = ["build"] } | ||
|
||
[dev-dependencies] | ||
uniffi-dart = { path = "../../", features = ["bindgen-tests"] } | ||
uniffi = { workspace = true, features = ["bindgen-tests"] } | ||
anyhow = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
uniffi_dart::generate_scaffolding("./src/api.udl".into()).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
namespace duration_type_test { }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use core::time::Duration; | ||
use uniffi; | ||
|
||
#[uniffi::export] | ||
pub fn make_duration(seconds: u64, nanos: u32) -> Duration { | ||
Duration::new(seconds, nanos) | ||
} | ||
|
||
#[uniffi::export] | ||
pub fn get_seconds(duration: Duration) -> u64 { | ||
duration.as_secs() | ||
} | ||
|
||
#[uniffi::export] | ||
pub fn get_nanos(duration: Duration) -> u32 { | ||
duration.subsec_nanos() | ||
} | ||
|
||
uniffi::include_scaffolding!("api"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:test/test.dart'; | ||
import '../duration_type_test.dart'; | ||
|
||
void main() { | ||
final api = Api.load(); | ||
test('rust return value seconds check', () { | ||
final duration = api.makeDuration(5, 0); | ||
|
||
expect(duration.inSeconds, 5); | ||
expect(api.getSeconds(duration), 5); | ||
expect(api.getNanos(duration), 0); | ||
}); | ||
|
||
test('seconds data check from dart', () { | ||
final duration = Duration(seconds: 10); | ||
expect(api.getSeconds(duration), 10); | ||
expect(api.getNanos(duration), 0); | ||
}); | ||
|
||
test('check nanos/micros', () { | ||
final duration = api.makeDuration(0, 3000); | ||
expect(duration.inSeconds, 0); | ||
expect(duration.inMicroseconds, 3); | ||
expect(api.getSeconds(duration), 0); | ||
expect(api.getNanos(duration), 3000); | ||
}); | ||
|
||
test('check large values', () { | ||
final duration = api.makeDuration(123456789, 3000000); | ||
expect(duration.inSeconds, 123456789); | ||
expect(duration.inMicroseconds, 123456789003000); | ||
expect(api.getSeconds(duration), 123456789); | ||
expect(api.getNanos(duration), 3000000); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use anyhow::Result; | ||
|
||
#[test] | ||
fn duration_type_test() -> Result<()> { | ||
uniffi_dart::testing::run_test("duration_type_test", "src/api.udl", None) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::gen::{ | ||
quote, | ||
render::{Renderable, TypeHelperRenderer}, | ||
}; | ||
|
||
use super::paste; | ||
use genco::lang::dart; | ||
|
||
impl_code_type_for_primitive!(DurationCodeType, "duration", "Duration"); | ||
|
||
impl Renderable for DurationCodeType { | ||
fn render_type_helper(&self, _type_helper: &dyn TypeHelperRenderer) -> dart::Tokens { | ||
quote! { | ||
class FfiConverterDuration { | ||
static Duration lift(Api api, RustBuffer buf) { | ||
return FfiConverterDuration.read(api, buf.asUint8List()).value; | ||
} | ||
|
||
static RustBuffer lower(Api api, Duration value) { | ||
final buf = Uint8List(12); | ||
FfiConverterDuration.write(api, value, buf); | ||
return toRustBuffer(api, buf); | ||
} | ||
|
||
static LiftRetVal<Duration> read(Api api, Uint8List buf) { | ||
final bytes = buf.buffer.asByteData(buf.offsetInBytes, 12); | ||
final seconds = bytes.getUint64(0); | ||
final micros = (bytes.getUint32(8) ~/ 1000); | ||
return LiftRetVal(Duration(seconds: seconds, microseconds: micros), 12); | ||
} | ||
|
||
static int allocationSize([Duration value = const Duration()]) { | ||
return 12; | ||
} | ||
|
||
static int write(Api api, Duration value, Uint8List buf) { | ||
final bytes = buf.buffer.asByteData(buf.offsetInBytes, 12); | ||
bytes.setUint64(0, value.inSeconds); | ||
final ms = (value.inMicroseconds - (value.inSeconds * 1000000)) * 1000; | ||
if (ms > 0) { | ||
bytes.setUint32(8, ms.toInt()); | ||
} | ||
return 12; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters