pub fn with_vector<A: Allocator, R>(
values: &mut Vec<u64, A>,
limit: usize,
operation: impl for<'scope> FnOnce(Vector<'scope, A>) -> R,
) -> Result<R, Refusal>Expand description
Lend a restricted view while keeping the owning Vec with the caller.
An oversized initial vector refuses before invoking the callback. Ordinary returned errors preserve preceding writes. This is not a transaction or a persistence admission API. A native host must arrange its own poisoning before invoking user code; IC callers must trap on unexpected failure. Forgetting the view does not leak the vector or leave an ownership ticket.
use ic_storage::with_vector;
let mut values = vec![17, 23];
let sum = with_vector(&mut values, 512, |mut view| {
view.push(41).unwrap();
view.values().iter().sum::<u64>()
}).unwrap();
assert_eq!(sum, 81);A view cannot escape through a return value:
ⓘ
use ic_storage::with_vector;
let mut values = vec![17];
let escaped = with_vector(&mut values, 512, |view| view);An outer capture cannot retain it either:
ⓘ
use ic_storage::with_vector;
let mut values = vec![17];
let mut escaped = None;
with_vector(&mut values, 512, |view| escaped = Some(view));Nor can a future carry it out of the scope:
ⓘ
use ic_storage::with_vector;
let mut values = vec![17];
let future = with_vector(&mut values, 512, |view| async move { view.values().len() });