Struct rug::integer::MiniInteger

source ·
pub struct MiniInteger { /* private fields */ }
Expand description

A small integer that does not require any memory allocation.

This can be useful when you have a primitive integer type such as u64 or i8, but need a reference to an Integer.

If there are functions that take a u32 or i32 directly instead of an Integer reference, using them can still be faster than using a MiniInteger; the functions would still need to check for the size of an Integer obtained using MiniInteger.

The borrow method returns an object that can be coerced to an Integer, as it implements Deref<Target = Integer>.

Examples

use rug::integer::MiniInteger;
use rug::Integer;
// `a` requires a heap allocation
let mut a = Integer::from(250);
// `b` can reside on the stack
let b = MiniInteger::from(-100);
a.lcm_mut(&b.borrow());
assert_eq!(a, 500);
// another computation:
a.lcm_mut(&MiniInteger::from(30).borrow());
assert_eq!(a, 1500);

Implementations§

source§

impl MiniInteger

source

pub const fn new() -> Self

Creates a MiniInteger with value 0.

Examples
use rug::integer::MiniInteger;
let i = MiniInteger::new();
// Borrow i as if it were Integer.
assert_eq!(*i.borrow(), 0);
source

pub unsafe fn as_nonreallocating_integer(&mut self) -> &mut Integer

Returns a mutable reference to an Integer for simple operations that do not need to allocate more space for the number.

Safety

It is undefined behavior to perform operations that reallocate the internal data of the referenced Integer or to swap it with another number.

Some GMP functions swap the allocations of their target operands; calling such functions with the mutable reference returned by this method can lead to undefined behavior.

Examples
use rug::integer::MiniInteger;
use rug::Assign;
let mut i = MiniInteger::from(1u64);
let capacity = i.borrow().capacity();
// another u64 will not require a reallocation
unsafe {
    i.as_nonreallocating_integer().assign(2u64);
}
assert_eq!(*i.borrow(), 2);
assert_eq!(i.borrow().capacity(), capacity);
source

pub fn borrow(&self) -> impl Deref<Target = Integer> + '_

Borrows the integer.

The returned object implements Deref<Target = Integer>.

The borrow lasts until the returned object exits scope. Multiple borrows can be taken at the same time.

Examples
use rug::integer::MiniInteger;
use rug::Integer;
let i = MiniInteger::from(-13i32);
let b = i.borrow();
let abs_ref = b.abs_ref();
assert_eq!(Integer::from(abs_ref), 13);
source

pub fn borrow_excl(&mut self) -> &Integer

Borrows the integer exclusively.

This is similar to the borrow method, but it requires exclusive access to the underlying MiniInteger; the returned reference can however be shared. The exclusive access is required to reduce the amount of housekeeping necessary, providing a more efficient operation.

Examples
use rug::integer::MiniInteger;
use rug::Integer;
let mut i = MiniInteger::from(-13i32);
let b = i.borrow_excl();
let abs_ref = b.abs_ref();
assert_eq!(Integer::from(abs_ref), 13);

Trait Implementations§

source§

impl Assign<&MiniInteger> for Integer

source§

fn assign(&mut self, src: &MiniInteger)

Peforms the assignement.
source§

impl Assign<&MiniInteger> for MiniInteger

source§

fn assign(&mut self, other: &Self)

Peforms the assignement.
source§

impl Assign<MiniInteger> for Integer

source§

fn assign(&mut self, src: MiniInteger)

Peforms the assignement.
source§

impl<T: ToMini> Assign<T> for MiniInteger

source§

fn assign(&mut self, src: T)

Peforms the assignement.
source§

impl Assign for MiniInteger

source§

fn assign(&mut self, other: Self)

Peforms the assignement.
source§

impl Binary for MiniInteger

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl Clone for MiniInteger

source§

fn clone(&self) -> MiniInteger

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for MiniInteger

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
source§

impl Default for MiniInteger

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Display for MiniInteger

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
source§

impl From<&MiniInteger> for Integer

source§

fn from(src: &MiniInteger) -> Self

Converts to this type from the input type.
source§

impl From<MiniInteger> for Integer

source§

fn from(src: MiniInteger) -> Self

Converts to this type from the input type.
source§

impl<T: ToMini> From<T> for MiniInteger

source§

fn from(src: T) -> Self

Converts to this type from the input type.
source§

impl LowerHex for MiniInteger

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl Octal for MiniInteger

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl PartialEq<Integer> for MiniInteger

source§

fn eq(&self, other: &Integer) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<MiniInteger> for Integer

source§

fn eq(&self, other: &MiniInteger) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Integer> for MiniInteger

source§

fn partial_cmp(&self, other: &Integer) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<MiniInteger> for Integer

source§

fn partial_cmp(&self, other: &MiniInteger) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl UpperHex for MiniInteger

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter.
source§

impl Copy for MiniInteger

source§

impl Send for MiniInteger

source§

impl Sync for MiniInteger

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.