Merge rust-bitcoin/rust-bitcoin#3926: units: Test for dyn compatibility

289a521426 units: Test for dyn compatibility (Tobin C. Harding)

Pull request description:

  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

ACKs for top commit:
  apoelstra:
    ACK 289a5214265a1f0d7b320b0121775f868a386c57; successfully ran local tests; nice!

Tree-SHA512: 6f247d4977a87acf68a03bfae12335a8f16ebbe44478da757128f54697c859fa278ddbc0e5c4479918797db91dbe8eef3d2adf60f83518182cb10e84e77120da
This commit is contained in:
merge-script 2025-01-20 23:32:43 +00:00
commit 8ff69fe9b2
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
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
/// passes non-integer to the `parse()` function.
// This trait is not dyn-compatible because `FromStr` is not dyn-compatible.
pub trait Integer:
FromStr<Err = core::num::ParseIntError> + TryFrom<i8> + Sized + sealed::Sealed
{

View File

@ -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<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")]
impl<'a> Arbitrary<'a> for Types {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {