Skip to content

Commit 8bf2927

Browse files
committed
Merge #399: Clarified conversions between Parity and integers
705c9cf Clarified conversions between `Parity` and integers (Martin Habovstiak) Pull request description: This was discussed in #390 (comment) ACKs for top commit: apoelstra: ACK 705c9cf Tree-SHA512: 3ba2ec566099c3c6d1c6f830e4959312b818b8766d924e3d995e6b23bd196ab747cc03d46f494ef451569188b0163f53e3236cacd20bfae9118ee76bcdbc9c02
2 parents f97e41a + 705c9cf commit 8bf2927

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/key.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,22 +1201,32 @@ pub enum Parity {
12011201
}
12021202

12031203
impl Parity {
1204-
/// Converts parity into a integer (byte) value.
1204+
/// Converts parity into an integer (byte) value.
1205+
///
1206+
/// This returns `0` for even parity and `1` for odd parity.
12051207
pub fn to_u8(self) -> u8 {
12061208
self as u8
12071209
}
12081210

1209-
/// Converts parity into a integer value.
1211+
/// Converts parity into an integer value.
1212+
///
1213+
/// This returns `0` for even parity and `1` for odd parity.
12101214
pub fn to_i32(self) -> i32 {
12111215
self as i32
12121216
}
12131217

12141218
/// Constructs a [`Parity`] from a byte.
1219+
///
1220+
/// The only allowed values are `0` meaning even parity and `1` meaning odd.
1221+
/// Other values result in error being returned.
12151222
pub fn from_u8(parity: u8) -> Result<Parity, Error> {
1216-
Parity::from_i32(parity as i32)
1223+
Parity::from_i32(parity.into())
12171224
}
12181225

12191226
/// Constructs a [`Parity`] from a signed integer.
1227+
///
1228+
/// The only allowed values are `0` meaning even parity and `1` meaning odd.
1229+
/// Other values result in error being returned.
12201230
pub fn from_i32(parity: i32) -> Result<Parity, Error> {
12211231
match parity {
12221232
0 => Ok(Parity::Even),
@@ -1228,7 +1238,9 @@ impl Parity {
12281238

12291239
impl From<i32> for Parity {
12301240
/// Please note, this method is deprecated and will be removed in an upcoming release, it
1231-
/// is not equivalent to `from_u32()`, it is better to use `Parity::from_u32`.
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.
12321244
fn from(parity: i32) -> Parity {
12331245
if parity % 2 == 0 {
12341246
Parity::Even
@@ -1238,12 +1250,14 @@ impl From<i32> for Parity {
12381250
}
12391251
}
12401252

1253+
/// The conversion returns `0` for even parity and `1` for odd.
12411254
impl From<Parity> for i32 {
12421255
fn from(parity: Parity) -> i32 {
12431256
parity.to_i32()
12441257
}
12451258
}
12461259

1260+
/// Returns even parity if the operands are equal, odd otherwise.
12471261
impl BitXor for Parity {
12481262
type Output = Parity;
12491263

@@ -1257,6 +1271,7 @@ impl BitXor for Parity {
12571271
}
12581272
}
12591273

1274+
/// The parity is serialized as `i32` - `0` for even, `1` for odd.
12601275
#[cfg(feature = "serde")]
12611276
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
12621277
impl ::serde::Serialize for Parity {
@@ -1265,6 +1280,7 @@ impl ::serde::Serialize for Parity {
12651280
}
12661281
}
12671282

1283+
/// The parity is deserialized as `i32` - `0` for even, `1` for odd.
12681284
#[cfg(feature = "serde")]
12691285
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
12701286
impl<'de> ::serde::Deserialize<'de> for Parity {

0 commit comments

Comments
 (0)