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 const fn const_from_bool(val: bool) -> Self
pub const fn const_from_bool(val: bool) -> Self
Creates a MiniInteger
from a bool
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const ONE_MINI: MiniInteger = MiniInteger::const_from_bool(true);
const ONE_BORROW: BorrowInteger = ONE_MINI.borrow();
const ONE: &Integer = BorrowInteger::const_deref(&ONE_BORROW);
assert_eq!(*ONE, 1);
sourcepub const fn const_from_i8(val: i8) -> Self
pub const fn const_from_i8(val: i8) -> Self
Creates a MiniInteger
from a i8
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const TWO_MINI: MiniInteger = MiniInteger::const_from_i8(2i8);
const TWO_BORROW: BorrowInteger = TWO_MINI.borrow();
const TWO: &Integer = BorrowInteger::const_deref(&TWO_BORROW);
assert_eq!(*TWO, 2);
sourcepub const fn const_from_i16(val: i16) -> Self
pub const fn const_from_i16(val: i16) -> Self
Creates a MiniInteger
from a i16
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const KIBI_MINI: MiniInteger = MiniInteger::const_from_i16(1i16 << 10);
const KIBI_BORROW: BorrowInteger = KIBI_MINI.borrow();
const KIBI: &Integer = BorrowInteger::const_deref(&KIBI_BORROW);
assert_eq!(*KIBI, 1i16 << 10);
sourcepub const fn const_from_i32(val: i32) -> Self
pub const fn const_from_i32(val: i32) -> Self
Creates a MiniInteger
from a i32
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const MEBI_MINI: MiniInteger = MiniInteger::const_from_i32(1i32 << 20);
const MEBI_BORROW: BorrowInteger = MEBI_MINI.borrow();
const MEBI: &Integer = BorrowInteger::const_deref(&MEBI_BORROW);
assert_eq!(*MEBI, 1i32 << 20);
sourcepub const fn const_from_i64(val: i64) -> Self
pub const fn const_from_i64(val: i64) -> Self
Creates a MiniInteger
from a i64
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const TEBI_MINI: MiniInteger = MiniInteger::const_from_i64(1i64 << 40);
const TEBI_BORROW: BorrowInteger = TEBI_MINI.borrow();
const TEBI: &Integer = BorrowInteger::const_deref(&TEBI_BORROW);
assert_eq!(*TEBI, 1i64 << 40);
sourcepub const fn const_from_i128(val: i128) -> Self
pub const fn const_from_i128(val: i128) -> Self
Creates a MiniInteger
from a i128
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const YOBI_MINI: MiniInteger = MiniInteger::const_from_i128(1i128 << 80);
const YOBI_BORROW: BorrowInteger = YOBI_MINI.borrow();
const YOBI: &Integer = BorrowInteger::const_deref(&YOBI_BORROW);
assert_eq!(*YOBI, 1i128 << 80);
sourcepub const fn const_from_isize(val: isize) -> Self
pub const fn const_from_isize(val: isize) -> Self
Creates a MiniInteger
from a isize
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const KIBI_MINI: MiniInteger = MiniInteger::const_from_isize(1isize << 10);
const KIBI_BORROW: BorrowInteger = KIBI_MINI.borrow();
const KIBI: &Integer = BorrowInteger::const_deref(&KIBI_BORROW);
assert_eq!(*KIBI, 1isize << 10);
sourcepub const fn const_from_u8(val: u8) -> Self
pub const fn const_from_u8(val: u8) -> Self
Creates a MiniInteger
from a u8
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const TWO_MINI: MiniInteger = MiniInteger::const_from_u8(2u8);
const TWO_BORROW: BorrowInteger = TWO_MINI.borrow();
const TWO: &Integer = BorrowInteger::const_deref(&TWO_BORROW);
assert_eq!(*TWO, 2);
sourcepub const fn const_from_u16(val: u16) -> Self
pub const fn const_from_u16(val: u16) -> Self
Creates a MiniInteger
from a u16
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const KIBI_MINI: MiniInteger = MiniInteger::const_from_u16(1u16 << 10);
const KIBI_BORROW: BorrowInteger = KIBI_MINI.borrow();
const KIBI: &Integer = BorrowInteger::const_deref(&KIBI_BORROW);
assert_eq!(*KIBI, 1u16 << 10);
sourcepub const fn const_from_u32(val: u32) -> Self
pub const fn const_from_u32(val: u32) -> Self
Creates a MiniInteger
from a u32
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const MEBI_MINI: MiniInteger = MiniInteger::const_from_u32(1u32 << 20);
const MEBI_BORROW: BorrowInteger = MEBI_MINI.borrow();
const MEBI: &Integer = BorrowInteger::const_deref(&MEBI_BORROW);
assert_eq!(*MEBI, 1u32 << 20);
sourcepub const fn const_from_u64(val: u64) -> Self
pub const fn const_from_u64(val: u64) -> Self
Creates a MiniInteger
from a u64
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const TEBI_MINI: MiniInteger = MiniInteger::const_from_u64(1u64 << 40);
const TEBI_BORROW: BorrowInteger = TEBI_MINI.borrow();
const TEBI: &Integer = BorrowInteger::const_deref(&TEBI_BORROW);
assert_eq!(*TEBI, 1u64 << 40);
sourcepub const fn const_from_u128(val: u128) -> Self
pub const fn const_from_u128(val: u128) -> Self
Creates a MiniInteger
from a u128
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const YOBI_MINI: MiniInteger = MiniInteger::const_from_u128(1u128 << 80);
const YOBI_BORROW: BorrowInteger = YOBI_MINI.borrow();
const YOBI: &Integer = BorrowInteger::const_deref(&YOBI_BORROW);
assert_eq!(*YOBI, 1u128 << 80);
sourcepub const fn const_from_usize(val: usize) -> Self
pub const fn const_from_usize(val: usize) -> Self
Creates a MiniInteger
from a usize
.
This is equivalent to MiniInteger::from(val)
, but can also be used in
constant context. Unless required in constant context, use the From
trait instead.
§Planned deprecation
This method will be deprecated when the From
trait is usable in
constant context.
§Examples
use rug::integer::{BorrowInteger, MiniInteger};
use rug::Integer;
const KIBI_MINI: MiniInteger = MiniInteger::const_from_usize(1usize << 10);
const KIBI_BORROW: BorrowInteger = KIBI_MINI.borrow();
const KIBI: &Integer = BorrowInteger::const_deref(&KIBI_BORROW);
assert_eq!(*KIBI, 1usize << 10);
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 const fn borrow(&self) -> BorrowInteger<'_>
pub const fn borrow(&self) -> BorrowInteger<'_>
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 Assign<MiniInteger> for MiniRational
impl Assign<MiniInteger> for MiniRational
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 From<MiniInteger> for MiniRational
impl From<MiniInteger> for MiniRational
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