From 8dc13ac3455fac12e5ad8d7ccbd043b7c31e2f9e Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 26 Aug 2013 13:29:33 -0700 Subject: [PATCH] std: Make vec::from_fn failure-safe --- src/libstd/vec.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index b743a17b472..976a3cafb32 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -75,6 +75,7 @@ use rt::global_heap::realloc_raw; use sys; use sys::size_of; use uint; +use unstable::finally::Finally; use unstable::intrinsics; use unstable::intrinsics::{get_tydesc, contains_managed}; use unstable::raw::{Box, Repr, Slice, Vec}; @@ -97,11 +98,14 @@ pub fn from_fn(n_elts: uint, op: &fn(uint) -> T) -> ~[T] { let mut v = with_capacity(n_elts); let p = raw::to_mut_ptr(v); let mut i: uint = 0u; - while i < n_elts { - intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i as int)), op(i)); - i += 1u; + do (|| { + while i < n_elts { + intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i as int)), op(i)); + i += 1u; + } + }).finally { + raw::set_len(&mut v, i); } - raw::set_len(&mut v, n_elts); v } }