-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for duration primitive type in code generation #33
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d5ab851
Add support for duration primitive type in code generation
gtalha07 093f22c
add tests and missing type generator for lifter and lower fns
gtalha07 299aeba
some minor corrections on duration type conversion
gtalha07 d766df7
restructure duration primitive type generators
gtalha07 81d1d53
tests correction
gtalha07 57284df
fixes around converter methods
gtalha07 0c243f4
fix type annotation error
gnunicorn 102dcb0
finalize Duration support
gnunicorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,6 +365,7 @@ pub fn generate_type(ty: &Type) -> dart::Tokens { | |
| Type::Int64 | ||
| Type::UInt16 | ||
| Type::Int32 | ||
| Type::Duration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure this is needed at all.... |
||
| Type::UInt64 => quote!(int), | ||
Type::Float32 | Type::Float64 => quote!(double), | ||
Type::String => quote!(String), | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neither that this is actually needed.