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
This commit is contained in:
Tobin C. Harding 2025-01-20 09:36:23 +11:00
parent 3985333e23
commit 289a521426
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 13 additions and 0 deletions

View File

@ -53,6 +53,7 @@ impl AsRef<core::num::ParseIntError> for ParseIntError {
/// Not strictly necessary but serves as a lint - avoids weird behavior if someone accidentally /// Not strictly necessary but serves as a lint - avoids weird behavior if someone accidentally
/// passes non-integer to the `parse()` function. /// passes non-integer to the `parse()` function.
// This trait is not dyn-compatible because `FromStr` is not dyn-compatible.
pub trait Integer: pub trait Integer:
FromStr<Err = core::num::ParseIntError> + TryFrom<i8> + Sized + sealed::Sealed FromStr<Err = core::num::ParseIntError> + TryFrom<i8> + Sized + sealed::Sealed
{ {

View File

@ -247,6 +247,18 @@ fn regression_default() {
assert_eq!(got, want); assert_eq!(got, want);
} }
#[test]
fn dyn_compatible() {
// If this builds then traits are dyn compatible.
struct Traits {
a: Box<dyn amount::CheckedSum<Amount>>,
// These traits are explicitly not dyn compatible.
// b: Box<dyn amount::serde::SerdeAmount>,
// c: Box<dyn amount::serde::SerdeAmountForOpt>,
// d: Box<dyn parse::Integer>,
}
}
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for Types { impl<'a> Arbitrary<'a> for Types {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {