rune_alloc::limit

Struct Memory

Source
pub struct Memory<T> { /* private fields */ }
Expand description

Something being budgeted.

See with.

Implementations§

Source§

impl<T> Memory<T>
where T: Callable,

Source

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> Callable for Memory<T>
where T: Callable,

Source§

type Output = <T as Callable>::Output

Output of the callable.
Source§

fn call(self) -> Self::Output

Call and consume the callable.
Source§

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);
Source§

type Output = <T as Future>::Output

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
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<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<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

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

Source§

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>,

Source§

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.