Skip to content

Commit 289a521

Browse files
committed
units: Test for dyn compatibility
Phew! dyn compatibility is a non-trivial concept. There are four public traits in `units`, only one is dyn compatible. This patch is done in order to check off C-OBJECT from the Rust API guidelines checklist. Add a test to check the public traits in `units` for dyn compatibility. While we are at it add a code comment on `Integer` stating why its not dyn-compatible. ref: https://rust-lang.github.io/api-guidelines/flexibility.html#c-object
1 parent 3985333 commit 289a521

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

units/src/parse.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl AsRef<core::num::ParseIntError> for ParseIntError {
5353

5454
/// Not strictly necessary but serves as a lint - avoids weird behavior if someone accidentally
5555
/// passes non-integer to the `parse()` function.
56+
// This trait is not dyn-compatible because `FromStr` is not dyn-compatible.
5657
pub trait Integer:
5758
FromStr<Err = core::num::ParseIntError> + TryFrom<i8> + Sized + sealed::Sealed
5859
{

units/tests/api.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ fn regression_default() {
247247
assert_eq!(got, want);
248248
}
249249

250+
#[test]
251+
fn dyn_compatible() {
252+
// If this builds then traits are dyn compatible.
253+
struct Traits {
254+
a: Box<dyn amount::CheckedSum<Amount>>,
255+
// These traits are explicitly not dyn compatible.
256+
// b: Box<dyn amount::serde::SerdeAmount>,
257+
// c: Box<dyn amount::serde::SerdeAmountForOpt>,
258+
// d: Box<dyn parse::Integer>,
259+
}
260+
}
261+
250262
#[cfg(feature = "arbitrary")]
251263
impl<'a> Arbitrary<'a> for Types {
252264
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {

0 commit comments

Comments
 (0)