-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add timestamp and duration types (#9)
* Add timestamp and duration types Along with `uniffi-fixture-time` to test them * Add codeowners file
- Loading branch information
Showing
10 changed files
with
244 additions
and
22 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,2 @@ | ||
* @IronCoreLabs/rust-dev | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,30 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
use super::CodeType; | ||
use crate::ComponentInterface; | ||
use paste::paste; | ||
|
||
macro_rules! impl_code_type_for_miscellany { | ||
($T:ty, $class_name:literal, $canonical_name:literal) => { | ||
paste! { | ||
#[derive(Debug)] | ||
pub struct $T; | ||
|
||
impl CodeType for $T { | ||
fn type_label(&self, _ci: &ComponentInterface) -> String { | ||
$class_name.into() | ||
} | ||
|
||
fn canonical_name(&self) -> String { | ||
$canonical_name.into() | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_code_type_for_miscellany!(TimestampCodeType, "java.time.Instant", "Timestamp"); | ||
|
||
impl_code_type_for_miscellany!(DurationCodeType, "java.time.Duration", "Duration"); |
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,50 @@ | ||
package {{ config.package_name() }}; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.time.Instant; | ||
import java.time.Duration; | ||
import java.time.DateTimeException; | ||
|
||
public enum FfiConverterDuration implements FfiConverterRustBuffer<Duration> { | ||
INSTANCE; | ||
|
||
@Override | ||
public Duration read(ByteBuffer buf) { | ||
// Type mismatch (should be u64) but we check for overflow/underflow below | ||
long seconds = buf.getLong(); | ||
// Type mismatch (should be u32) but we check for overflow/underflow below | ||
long nanoseconds = (long) buf.getInt(); | ||
if (seconds < 0) { | ||
throw new DateTimeException("Duration exceeds minimum or maximum value supported by uniffi"); | ||
} | ||
if (nanoseconds < 0) { | ||
throw new DateTimeException("Duration nanoseconds exceed minimum or maximum supported by uniffi"); | ||
} | ||
return Duration.ofSeconds(seconds, nanoseconds); | ||
} | ||
|
||
// 8 bytes for seconds, 4 bytes for nanoseconds | ||
@Override | ||
public long allocationSize(Duration value) { | ||
return 12L; | ||
} | ||
|
||
@Override | ||
public void write(Duration value, ByteBuffer buf) { | ||
if (value.getSeconds() < 0) { | ||
// Rust does not support negative Durations | ||
throw new IllegalArgumentException("Invalid duration, must be non-negative"); | ||
} | ||
|
||
if (value.getNano() < 0) { | ||
// Java docs provide guarantee that nano will always be positive, so this should be impossible | ||
// See: https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html | ||
throw new IllegalArgumentException("Invalid duration, nano value must be non-negative"); | ||
} | ||
|
||
// Type mismatch (should be u64) but since Rust doesn't support negative durations we should be OK | ||
buf.putLong(value.getSeconds()); | ||
// Type mismatch (should be u32) but since values will always be between 0 and 999,999,999 it should be OK | ||
buf.putInt(value.getNano()); | ||
} | ||
} |
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,52 @@ | ||
package {{ config.package_name() }}; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.time.Instant; | ||
import java.time.Duration; | ||
import java.time.DateTimeException; | ||
|
||
public enum FfiConverterTimestamp implements FfiConverterRustBuffer<Instant> { | ||
INSTANCE; | ||
|
||
@Override | ||
public Instant read(ByteBuffer buf) { | ||
long seconds = buf.getLong(); | ||
// Type mismatch (should be u32) but we check for overflow/underflow below | ||
long nanoseconds = (long) buf.getInt(); | ||
if (nanoseconds < 0) { | ||
throw new DateTimeException("Instant nanoseconds exceed minimum or maximum supported by uniffi"); | ||
} | ||
if (seconds >= 0) { | ||
return Instant.EPOCH.plus(Duration.ofSeconds(seconds, nanoseconds)); | ||
} else { | ||
return Instant.EPOCH.minus(Duration.ofSeconds(-seconds, nanoseconds)); | ||
} | ||
} | ||
|
||
// 8 bytes for seconds, 4 bytes for nanoseconds | ||
@Override | ||
public long allocationSize(Instant value) { | ||
return 12L; | ||
} | ||
|
||
@Override | ||
public void write(Instant value, ByteBuffer buf) { | ||
Duration epochOffset = Duration.between(Instant.EPOCH, value); | ||
|
||
var sign = 1; | ||
if (epochOffset.isNegative()) { | ||
sign = -1; | ||
epochOffset = epochOffset.negated(); | ||
} | ||
|
||
if (epochOffset.getNano() < 0) { | ||
// Java docs provide guarantee that nano will always be positive, so this should be impossible | ||
// See: https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html | ||
throw new IllegalArgumentException("Invalid timestamp, nano value must be non-negative"); | ||
} | ||
|
||
buf.putLong(sign * epochOffset.getSeconds()); | ||
// Type mismatch (should be u32) but since values will always be between 0 and 999,999,999 it should be OK | ||
buf.putInt(epochOffset.getNano()); | ||
} | ||
} |
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
Oops, something went wrong.