From 289a5214265a1f0d7b320b0121775f868a386c57 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 20 Jan 2025 09:36:23 +1100 Subject: [PATCH] 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 --- units/src/parse.rs | 1 + units/tests/api.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/units/src/parse.rs b/units/src/parse.rs index 6127a8f9e..b3a6598e2 100644 --- a/units/src/parse.rs +++ b/units/src/parse.rs @@ -53,6 +53,7 @@ impl AsRef for ParseIntError { /// Not strictly necessary but serves as a lint - avoids weird behavior if someone accidentally /// passes non-integer to the `parse()` function. +// This trait is not dyn-compatible because `FromStr` is not dyn-compatible. pub trait Integer: FromStr + TryFrom + Sized + sealed::Sealed { diff --git a/units/tests/api.rs b/units/tests/api.rs index 0fec691bd..00ad9b084 100644 --- a/units/tests/api.rs +++ b/units/tests/api.rs @@ -247,6 +247,18 @@ fn regression_default() { assert_eq!(got, want); } +#[test] +fn dyn_compatible() { + // If this builds then traits are dyn compatible. + struct Traits { + a: Box>, + // These traits are explicitly not dyn compatible. + // b: Box, + // c: Box, + // d: Box, + } +} + #[cfg(feature = "arbitrary")] impl<'a> Arbitrary<'a> for Types { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result {