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
impl MiniInteger
sourcepub const fn new() -> Self
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);
sourcepub unsafe fn as_nonreallocating_integer(&mut self) -> &mut Integer
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);
sourcepub fn borrow(&self) -> impl Deref<Target = Integer> + '_
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);
sourcepub fn borrow_excl(&mut self) -> &Integer
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
impl Assign<&MiniInteger> for Integer
source§fn assign(&mut self, src: &MiniInteger)
fn assign(&mut self, src: &MiniInteger)
source§impl Assign<&MiniInteger> for MiniInteger
impl Assign<&MiniInteger> for MiniInteger
source§impl Assign<MiniInteger> for Integer
impl Assign<MiniInteger> for Integer
source§fn assign(&mut self, src: MiniInteger)
fn assign(&mut self, src: MiniInteger)
source§impl<T: ToMini> Assign<T> for MiniInteger
impl<T: ToMini> Assign<T> for MiniInteger
source§impl Binary for MiniInteger
impl Binary for MiniInteger
source§impl Clone for MiniInteger
impl Clone for MiniInteger
source§fn clone(&self) -> MiniInteger
fn clone(&self) -> MiniInteger
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for MiniInteger
impl Debug for MiniInteger
source§impl Default for MiniInteger
impl Default for MiniInteger
source§impl Display for MiniInteger
impl Display for MiniInteger
source§impl From<&MiniInteger> for Integer
impl From<&MiniInteger> for Integer
source§fn from(src: &MiniInteger) -> Self
fn from(src: &MiniInteger) -> Self
source§impl From<MiniInteger> for Integer
impl From<MiniInteger> for Integer
source§fn from(src: MiniInteger) -> Self
fn from(src: MiniInteger) -> Self
source§impl<T: ToMini> From<T> for MiniInteger
impl<T: ToMini> From<T> for MiniInteger
source§impl LowerHex for MiniInteger
impl LowerHex for MiniInteger
source§impl Octal for MiniInteger
impl Octal for MiniInteger
source§impl PartialEq<Integer> for MiniInteger
impl PartialEq<Integer> for MiniInteger
source§impl PartialEq<MiniInteger> for Integer
impl PartialEq<MiniInteger> for Integer
source§fn eq(&self, other: &MiniInteger) -> bool
fn eq(&self, other: &MiniInteger) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<Integer> for MiniInteger
impl PartialOrd<Integer> for MiniInteger
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<MiniInteger> for Integer
impl PartialOrd<MiniInteger> for Integer
source§fn partial_cmp(&self, other: &MiniInteger) -> Option<Ordering>
fn partial_cmp(&self, other: &MiniInteger) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more