File tree 1 file changed +30
-4
lines changed
1 file changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -125,11 +125,14 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] {
125
125
let mut v = with_capacity ( n_elts) ;
126
126
let p = raw:: to_mut_ptr ( v) ;
127
127
let mut i = 0 u;
128
- while i < n_elts {
129
- intrinsics:: move_val_init ( & mut ( * ptr:: mut_offset ( p, i as int ) ) , t. clone ( ) ) ;
130
- i += 1 u;
128
+ do ( || {
129
+ while i < n_elts {
130
+ intrinsics:: move_val_init ( & mut ( * ptr:: mut_offset ( p, i as int ) ) , t. clone ( ) ) ;
131
+ i += 1 u;
132
+ }
133
+ } ) . finally {
134
+ raw:: set_len ( & mut v, i) ;
131
135
}
132
- raw:: set_len ( & mut v, n_elts) ;
133
136
v
134
137
}
135
138
}
@@ -3134,6 +3137,29 @@ mod tests {
3134
3137
} ;
3135
3138
}
3136
3139
3140
+ #[ test]
3141
+ #[ should_fail]
3142
+ fn test_from_elem_fail ( ) {
3143
+ use cast;
3144
+
3145
+ struct S {
3146
+ f : int ,
3147
+ boxes : ( ~int , @int )
3148
+ }
3149
+
3150
+ impl Clone for S {
3151
+ fn clone ( & self ) -> S {
3152
+ let s = unsafe { cast:: transmute_mut ( self ) } ;
3153
+ s. f += 1 ;
3154
+ if s. f == 10 { fail ! ( ) }
3155
+ S { f : s. f , boxes : s. boxes . clone ( ) }
3156
+ }
3157
+ }
3158
+
3159
+ let s = S { f : 0 , boxes : ( ~0 , @0 ) } ;
3160
+ let _ = from_elem ( 100 , s) ;
3161
+ }
3162
+
3137
3163
#[ test]
3138
3164
#[ should_fail]
3139
3165
fn test_build_fail ( ) {
You can’t perform that action at this time.
0 commit comments