@@ -1134,7 +1134,7 @@ impl XOnlyPublicKey {
1134
1134
return Err ( Error :: InvalidPublicKey ) ;
1135
1135
}
1136
1136
1137
- Parity :: from_i32 ( parity)
1137
+ Parity :: from_i32 ( parity) . map_err ( Into :: into )
1138
1138
}
1139
1139
}
1140
1140
@@ -1219,33 +1219,19 @@ impl Parity {
1219
1219
///
1220
1220
/// The only allowed values are `0` meaning even parity and `1` meaning odd.
1221
1221
/// Other values result in error being returned.
1222
- pub fn from_u8 ( parity : u8 ) -> Result < Parity , Error > {
1222
+ pub fn from_u8 ( parity : u8 ) -> Result < Parity , InvalidParityValue > {
1223
1223
Parity :: from_i32 ( parity. into ( ) )
1224
1224
}
1225
1225
1226
1226
/// Constructs a [`Parity`] from a signed integer.
1227
1227
///
1228
1228
/// The only allowed values are `0` meaning even parity and `1` meaning odd.
1229
1229
/// Other values result in error being returned.
1230
- pub fn from_i32 ( parity : i32 ) -> Result < Parity , Error > {
1230
+ pub fn from_i32 ( parity : i32 ) -> Result < Parity , InvalidParityValue > {
1231
1231
match parity {
1232
1232
0 => Ok ( Parity :: Even ) ,
1233
1233
1 => Ok ( Parity :: Odd ) ,
1234
- _ => Err ( Error :: InvalidParityValue ) ,
1235
- }
1236
- }
1237
- }
1238
-
1239
- impl From < i32 > for Parity {
1240
- /// Please note, this method is deprecated and will be removed in an upcoming release, it
1241
- /// is **not** equivalent to `from_u32()`, it is better to use `Parity::from_u32`.
1242
- ///
1243
- /// This method returns same parity as the parity of input integer.
1244
- fn from ( parity : i32 ) -> Parity {
1245
- if parity % 2 == 0 {
1246
- Parity :: Even
1247
- } else {
1248
- Parity :: Odd
1234
+ _ => Err ( InvalidParityValue ( parity) ) ,
1249
1235
}
1250
1236
}
1251
1237
}
@@ -1257,6 +1243,13 @@ impl From<Parity> for i32 {
1257
1243
}
1258
1244
}
1259
1245
1246
+ /// The conversion returns `0` for even parity and `1` for odd.
1247
+ impl From < Parity > for u8 {
1248
+ fn from ( parity : Parity ) -> u8 {
1249
+ parity. to_u8 ( )
1250
+ }
1251
+ }
1252
+
1260
1253
/// Returns even parity if the operands are equal, odd otherwise.
1261
1254
impl BitXor for Parity {
1262
1255
type Output = Parity ;
@@ -1271,6 +1264,30 @@ impl BitXor for Parity {
1271
1264
}
1272
1265
}
1273
1266
1267
+ /// Error returned when conversion from an integer to `Parity` fails.
1268
+ //
1269
+ // Note that we don't allow inspecting the value because we may change the type.
1270
+ // Yes, this comment is intentionally NOT doc comment.
1271
+ // Too many derives for compatibility with current Error type.
1272
+ #[ derive( Copy , Clone , Debug , Eq , PartialEq , Hash , Ord , PartialOrd ) ]
1273
+ pub struct InvalidParityValue ( i32 ) ;
1274
+
1275
+ impl fmt:: Display for InvalidParityValue {
1276
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1277
+ write ! ( f, "invalid value {} for Parity - must be 0 or 1" , self . 0 )
1278
+ }
1279
+ }
1280
+
1281
+ #[ cfg( feature = "std" ) ]
1282
+ #[ cfg_attr( docsrs, doc( cfg( feature = "std" ) ) ) ]
1283
+ impl :: std:: error:: Error for InvalidParityValue { }
1284
+
1285
+ impl From < InvalidParityValue > for Error {
1286
+ fn from ( error : InvalidParityValue ) -> Self {
1287
+ Error :: InvalidParityValue ( error)
1288
+ }
1289
+ }
1290
+
1274
1291
/// The parity is serialized as `u8` - `0` for even, `1` for odd.
1275
1292
#[ cfg( feature = "serde" ) ]
1276
1293
#[ cfg_attr( docsrs, doc( cfg( feature = "serde" ) ) ) ]
0 commit comments