From 3e4d00f90f7a19315dca1ea38ac102698c61c855 Mon Sep 17 00:00:00 2001 From: Hubert Bugaj Date: Thu, 8 Aug 2024 14:59:46 +0200 Subject: [PATCH] fix doctests; more macro --- src/shim/sector.rs | 60 +++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/shim/sector.rs b/src/shim/sector.rs index d08ebe77a9c8..65fe84454f27 100644 --- a/src/shim/sector.rs +++ b/src/shim/sector.rs @@ -38,9 +38,13 @@ pub type SectorNumber = fvm_shared4::sector::SectorNumber; /// // Create a correspndoning FVM3 RegisteredSealProof /// let fvm3_proof = fvm_shared3::sector::RegisteredSealProof::StackedDRG2KiBV1; /// +/// // Create a correspndoning FVM4 RegisteredSealProof +/// let fvm4_proof = fvm_shared4::sector::RegisteredSealProof::StackedDRG2KiBV1; +/// /// // Create a shim out of fvm2 proof, ensure conversions are correct /// let proof_shim = RegisteredSealProof::from(fvm2_proof); -/// assert_eq!(fvm3_proof, *proof_shim); +/// assert_eq!(fvm4_proof, *proof_shim); +/// assert_eq!(fvm3_proof, proof_shim.into()); /// assert_eq!(fvm2_proof, proof_shim.into()); /// ``` #[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Eq, PartialEq, Debug)] @@ -99,40 +103,36 @@ impl Deref for RegisteredSealProof { } } -impl From for RegisteredSealProof { - fn from(value: RegisteredSealProofV2) -> RegisteredSealProof { - let num_id: i64 = value.into(); - RegisteredSealProof(RegisteredSealProofV4::from(num_id)) - } -} - -impl From for RegisteredSealProofV2 { - fn from(value: RegisteredSealProof) -> RegisteredSealProofV2 { - let num_id: i64 = value.0.into(); - RegisteredSealProofV2::from(num_id) - } -} - -impl From for RegisteredSealProof { - fn from(value: RegisteredSealProofV3) -> RegisteredSealProof { - let num_id: i64 = value.into(); - RegisteredSealProof(RegisteredSealProofV4::from(num_id)) +impl From for RegisteredSealProof { + fn from(value: i64) -> Self { + RegisteredSealProof(RegisteredSealProofV4::from(value)) } } -impl From for RegisteredSealProof { - fn from(value: RegisteredSealProofV4) -> RegisteredSealProof { - let num_id: i64 = value.into(); - RegisteredSealProof(RegisteredSealProofV4::from(num_id)) - } +macro_rules! registered_seal_proof_conversion { + ($($internal:ty),+) => { + $( + impl From<$internal> for RegisteredSealProof { + fn from(value: $internal) -> Self { + let num_id: i64 = value.into(); + RegisteredSealProof::from(num_id) + } + } + impl From for $internal { + fn from(value: RegisteredSealProof) -> $internal { + let num_id: i64 = value.0.into(); + <$internal>::from(num_id) + } + } + )+ + }; } -impl From for RegisteredSealProofV4 { - fn from(value: RegisteredSealProof) -> RegisteredSealProofV4 { - let num_id: i64 = value.0.into(); - RegisteredSealProofV4::from(num_id) - } -} +registered_seal_proof_conversion!( + RegisteredSealProofV2, + RegisteredSealProofV3, + RegisteredSealProofV4 +); #[cfg(test)] impl quickcheck::Arbitrary for RegisteredSealProof {