pub struct Memory<T> { /* private fields */ }
Expand description
Something being budgeted.
See with
.
Implementations§
Source§impl<T> Memory<T>where
T: Callable,
impl<T> Memory<T>where
T: Callable,
Sourcepub fn call(self) -> T::Output
pub fn call(self) -> T::Output
Call the wrapped function, replacing the current budget and restoring it once the function call completes.
§Examples
use rune::alloc::limit;
use rune::alloc::{Box, Result};
use rune::alloc::alloc::AllocError;
const LIMIT: usize = 1024;
fn doit() -> Result<Box<[u8; 256]>, AllocError> {
Box::try_new([0u8; 256])
}
fn limited() -> Result<()> {
assert_eq!(limit::get(), LIMIT);
// Hold onto a 256 byte allocation.
let b = doit()?;
assert_eq!(limit::get(), LIMIT - 256);
// Drop the allocation, making the memory available again.
drop(b);
assert_eq!(limit::get(), LIMIT);
Ok(())
}
let inner = limit::with(LIMIT, limited);
assert_eq!(limit::get(), usize::MAX);
inner.call()?;
assert_eq!(limit::get(), usize::MAX);
Limit being restored after its been breached:
use rune::alloc::limit;
use rune::alloc::{Box, Result};
use rune::alloc::alloc::AllocError;
const LIMIT: usize = 128;
fn doit() -> Result<Box<[u8; 256]>, AllocError> {
Box::try_new([0u8; 256])
}
fn limited() -> Result<()> {
assert_eq!(limit::get(), LIMIT);
// Fail to allocate since we don't have enough memory available.
assert!(doit().is_err());
assert_eq!(limit::get(), LIMIT);
Ok(())
}
let inner = limit::with(LIMIT, limited);
assert_eq!(limit::get(), usize::MAX);
inner.call()?;
assert_eq!(limit::get(), usize::MAX);
Trait Implementations§
Source§impl<T> Future for Memory<T>where
T: Future,
impl<T> Future for Memory<T>where
T: Future,
Treat the current budget as a future, ensuring that the budget is suspended and restored as necessary when the future is being polled.
§Examples
use rune::alloc::limit;
use rune::alloc::{Box, Result};
use rune::alloc::alloc::AllocError;
const LIMIT: usize = 1024;
async fn doit() -> Result<Box<[u8; 256]>, AllocError> {
Box::try_new([0u8; 256])
}
async fn limited() -> Result<()> {
assert_eq!(limit::get(), LIMIT);
// Hold onto a 256 byte allocation.
let b = doit().await?;
assert_eq!(limit::get(), LIMIT - 256);
// Drop the allocation, making the memory available again.
drop(b);
assert_eq!(limit::get(), LIMIT);
Ok(())
}
let inner = limit::with(LIMIT, limited());
assert_eq!(limit::get(), usize::MAX);
inner.await?;
assert_eq!(limit::get(), usize::MAX);
Limit being restored after its been breached:
use rune::alloc::limit;
use rune::alloc::{Box, Result};
use rune::alloc::alloc::AllocError;
const LIMIT: usize = 128;
async fn doit() -> Result<Box<[u8; 256]>, AllocError> {
Box::try_new([0u8; 256])
}
async fn limited() -> Result<()> {
assert_eq!(limit::get(), LIMIT);
// Fail to allocate since we don't have enough memory available.
assert!(doit().await.is_err());
assert_eq!(limit::get(), LIMIT);
Ok(())
}
let inner = limit::with(LIMIT, limited());
assert_eq!(limit::get(), usize::MAX);
inner.await?;
assert_eq!(limit::get(), usize::MAX);
impl<'pin, T> Unpin for Memory<T>where
PinnedFieldsOf<__Memory<'pin, T>>: Unpin,
Auto Trait Implementations§
impl<T> Freeze for Memory<T>where
T: Freeze,
impl<T> RefUnwindSafe for Memory<T>where
T: RefUnwindSafe,
impl<T> Send for Memory<T>where
T: Send,
impl<T> Sync for Memory<T>where
T: Sync,
impl<T> UnwindSafe for Memory<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more