46 lines
985 B
Rust
46 lines
985 B
Rust
use crate::runtime::{Variable, acquire, release, natural_get, natural_set};
|
|
use super::natural;
|
|
|
|
pub struct Natural {
|
|
managed:bool,
|
|
addr:Variable,
|
|
}
|
|
impl Natural {
|
|
pub fn new() -> Self
|
|
{
|
|
Self {
|
|
managed:true,
|
|
addr:unsafe {acquire(natural())},
|
|
}
|
|
}
|
|
|
|
pub fn from(addr:Variable) -> Self
|
|
{
|
|
Self { managed:false, addr:addr }
|
|
}
|
|
|
|
pub fn with(value:u64) -> Self
|
|
{
|
|
let mut obj = Self::new();
|
|
obj.set(value);
|
|
return obj;
|
|
}
|
|
|
|
pub fn set(&mut self, value:u64)
|
|
{
|
|
unsafe { natural_set(self.addr, value) };
|
|
}
|
|
|
|
pub fn get(&self) -> u64
|
|
{
|
|
unsafe { natural_get(self.addr) }
|
|
}
|
|
}
|
|
impl std::ops::Deref for Natural {
|
|
type Target = Variable;
|
|
fn deref(&self) -> &Self::Target { return &self.addr; }
|
|
}
|
|
impl Drop for Natural {
|
|
fn drop(&mut self) { if self.managed { unsafe {release(self.addr)}; } }
|
|
}
|