Skip to content

Commit

Permalink
Merge #276
Browse files Browse the repository at this point in the history
276: Implement To/FromBytes r=cuviper a=cuviper



Co-authored-by: Josh Stone <cuviper@gmail.com>
  • Loading branch information
bors[bot] and cuviper authored Jul 21, 2023
2 parents 6f2b8e0 + 6d2ff5e commit b9db55d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ default-features = false
features = ["i128"]

[dependencies.num-traits]
version = "0.2.11"
version = "0.2.16"
default-features = false
features = ["i128"]

Expand Down
24 changes: 24 additions & 0 deletions src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,30 @@ impl BigInt {
}
}

impl num_traits::FromBytes for BigInt {
type Bytes = [u8];

fn from_be_bytes(bytes: &Self::Bytes) -> Self {
Self::from_signed_bytes_be(bytes)
}

fn from_le_bytes(bytes: &Self::Bytes) -> Self {
Self::from_signed_bytes_le(bytes)
}
}

impl num_traits::ToBytes for BigInt {
type Bytes = Vec<u8>;

fn to_be_bytes(&self) -> Self::Bytes {
self.to_signed_bytes_be()
}

fn to_le_bytes(&self) -> Self::Bytes {
self.to_signed_bytes_le()
}
}

#[test]
fn test_from_biguint() {
fn check(inp_s: Sign, inp_n: usize, ans_s: Sign, ans_n: usize) {
Expand Down
24 changes: 24 additions & 0 deletions src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,30 @@ impl BigUint {
}
}

impl num_traits::FromBytes for BigUint {
type Bytes = [u8];

fn from_be_bytes(bytes: &Self::Bytes) -> Self {
Self::from_bytes_be(bytes)
}

fn from_le_bytes(bytes: &Self::Bytes) -> Self {
Self::from_bytes_le(bytes)
}
}

impl num_traits::ToBytes for BigUint {
type Bytes = Vec<u8>;

fn to_be_bytes(&self) -> Self::Bytes {
self.to_bytes_be()
}

fn to_le_bytes(&self) -> Self::Bytes {
self.to_bytes_le()
}
}

pub(crate) trait IntDigits {
fn digits(&self) -> &[BigDigit];
fn digits_mut(&mut self) -> &mut Vec<BigDigit>;
Expand Down
36 changes: 15 additions & 21 deletions tests/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use std::{i16, i32, i64, i8, isize};
use std::{u16, u32, u64, u8, usize};

use num_integer::Integer;
use num_traits::{pow, FromPrimitive, Num, One, Pow, Signed, ToPrimitive, Zero};
use num_traits::{
pow, FromBytes, FromPrimitive, Num, One, Pow, Signed, ToBytes, ToPrimitive, Zero,
};

mod consts;
use crate::consts::*;
Expand Down Expand Up @@ -94,12 +96,9 @@ fn test_to_bytes_le() {
#[test]
fn test_to_signed_bytes_le() {
fn check(s: &str, result: Vec<u8>) {
assert_eq!(
BigInt::parse_bytes(s.as_bytes(), 10)
.unwrap()
.to_signed_bytes_le(),
result
);
let b = BigInt::parse_bytes(s.as_bytes(), 10).unwrap();
assert_eq!(b.to_signed_bytes_le(), result);
assert_eq!(<BigInt as ToBytes>::to_le_bytes(&b), result);
}

check("0", vec![0]);
Expand All @@ -115,10 +114,9 @@ fn test_to_signed_bytes_le() {
#[test]
fn test_from_signed_bytes_le() {
fn check(s: &[u8], result: &str) {
assert_eq!(
BigInt::from_signed_bytes_le(s),
BigInt::parse_bytes(result.as_bytes(), 10).unwrap()
);
let b = BigInt::parse_bytes(result.as_bytes(), 10).unwrap();
assert_eq!(BigInt::from_signed_bytes_le(s), b);
assert_eq!(<BigInt as FromBytes>::from_le_bytes(s), b);
}

check(&[], "0");
Expand All @@ -136,12 +134,9 @@ fn test_from_signed_bytes_le() {
#[test]
fn test_to_signed_bytes_be() {
fn check(s: &str, result: Vec<u8>) {
assert_eq!(
BigInt::parse_bytes(s.as_bytes(), 10)
.unwrap()
.to_signed_bytes_be(),
result
);
let b = BigInt::parse_bytes(s.as_bytes(), 10).unwrap();
assert_eq!(b.to_signed_bytes_be(), result);
assert_eq!(<BigInt as ToBytes>::to_be_bytes(&b), result);
}

check("0", vec![0]);
Expand All @@ -157,10 +152,9 @@ fn test_to_signed_bytes_be() {
#[test]
fn test_from_signed_bytes_be() {
fn check(s: &[u8], result: &str) {
assert_eq!(
BigInt::from_signed_bytes_be(s),
BigInt::parse_bytes(result.as_bytes(), 10).unwrap()
);
let b = BigInt::parse_bytes(result.as_bytes(), 10).unwrap();
assert_eq!(BigInt::from_signed_bytes_be(s), b);
assert_eq!(<BigInt as FromBytes>::from_be_bytes(s), b);
}

check(&[], "0");
Expand Down
20 changes: 10 additions & 10 deletions tests/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use std::{i128, u128};
use std::{u16, u32, u64, u8, usize};

use num_traits::{
pow, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow, ToPrimitive,
Zero,
pow, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromBytes, FromPrimitive, Num, One, Pow,
ToBytes, ToPrimitive, Zero,
};

mod consts;
Expand All @@ -27,10 +27,9 @@ mod macros;
#[test]
fn test_from_bytes_be() {
fn check(s: &str, result: &str) {
assert_eq!(
BigUint::from_bytes_be(s.as_bytes()),
BigUint::parse_bytes(result.as_bytes(), 10).unwrap()
);
let b = BigUint::parse_bytes(result.as_bytes(), 10).unwrap();
assert_eq!(BigUint::from_bytes_be(s.as_bytes()), b);
assert_eq!(<BigUint as FromBytes>::from_be_bytes(s.as_bytes()), b);
}
check("A", "65");
check("AA", "16705");
Expand All @@ -44,6 +43,7 @@ fn test_to_bytes_be() {
fn check(s: &str, result: &str) {
let b = BigUint::parse_bytes(result.as_bytes(), 10).unwrap();
assert_eq!(b.to_bytes_be(), s.as_bytes());
assert_eq!(<BigUint as ToBytes>::to_be_bytes(&b), s.as_bytes());
}
check("A", "65");
check("AA", "16705");
Expand All @@ -60,10 +60,9 @@ fn test_to_bytes_be() {
#[test]
fn test_from_bytes_le() {
fn check(s: &str, result: &str) {
assert_eq!(
BigUint::from_bytes_le(s.as_bytes()),
BigUint::parse_bytes(result.as_bytes(), 10).unwrap()
);
let b = BigUint::parse_bytes(result.as_bytes(), 10).unwrap();
assert_eq!(BigUint::from_bytes_le(s.as_bytes()), b);
assert_eq!(<BigUint as FromBytes>::from_le_bytes(s.as_bytes()), b);
}
check("A", "65");
check("AA", "16705");
Expand All @@ -77,6 +76,7 @@ fn test_to_bytes_le() {
fn check(s: &str, result: &str) {
let b = BigUint::parse_bytes(result.as_bytes(), 10).unwrap();
assert_eq!(b.to_bytes_le(), s.as_bytes());
assert_eq!(<BigUint as ToBytes>::to_le_bytes(&b), s.as_bytes());
}
check("A", "65");
check("AA", "16705");
Expand Down

0 comments on commit b9db55d

Please sign in to comment.