Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to_slice and to_vec functions #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/bigint.rs
Expand Up @@ -1540,6 +1540,42 @@ impl BigInt {
BigUint::from_radix_le(buf, radix).map(|u| BigInt::from_biguint(sign, u))
}

/// Returns the representation of the `BigInt` in little-endian base 2<sup>32</sup>.
///
/// # Examples
///
///
/// ```
/// use num_bigint::{Sign, BigInt, BigDigit};
///
/// let sign = Sign::Plus;
/// let slice: &[BigDigit] = &[1, 2, 3, 4];
/// let i = BigInt::from_slice(sign, slice);
/// assert_eq!(i.to_slice(), (sign, slice));
/// ```
#[inline]
pub fn to_slice(&self) -> (Sign, &[BigDigit]) {
(self.sign, self.data.to_slice())
}

/// Returns the representation of the `BigInt` in little-endian base 2<sup>32</sup>.
///
/// # Examples
///
///
/// ```
/// use num_bigint::{Sign, BigInt};
///
/// let sign = Sign::Plus;
/// let vec = vec![1, 2, 3, 4];
/// let i = BigInt::from_slice(sign, &vec);
/// assert_eq!(i.to_vec(), (sign, vec));
/// ```
#[inline]
pub fn to_vec(self) -> (Sign, Vec<BigDigit>) {
(self.sign, self.data.to_vec())
}

/// Returns the sign and the byte representation of the `BigInt` in big-endian byte order.
///
/// # Examples
Expand Down
34 changes: 34 additions & 0 deletions src/biguint.rs
Expand Up @@ -1539,6 +1539,40 @@ impl BigUint {
}
}

/// Returns the representation of the `BigUint` in little-endian base 2<sup>32</sup>.
///
/// # Examples
///
///
/// ```
/// use num_bigint::BigUint;
///
/// let slice = &[1, 2, 3, 4];
/// let i = BigUint::from_slice(slice);
/// assert_eq!(i.to_slice(), slice);
/// ```
#[inline]
pub fn to_slice(&self) -> &[BigDigit] {
&self.data
}

/// Returns the representation of the `BigUint` in little-endian base 2<sup>32</sup>.
///
/// # Examples
///
///
/// ```
/// use num_bigint::BigUint;
///
/// let slice = &[1, 2, 3, 4];
/// let i = BigUint::from_slice(slice);
/// assert_eq!(i.to_vec(), slice);
/// ```
#[inline]
pub fn to_vec(self) -> Vec<BigDigit> {
self.data
}

/// Returns the integer formatted as a string in the given radix.
/// `radix` must be in the range `2...36`.
///
Expand Down