Skip to content

Commit 731e9ba

Browse files
committed
A new test: illustrate use of box with emplacing into back of Vec.
1 parent 452ecab commit 731e9ba

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This test illustrates use of `box (<place>) <value>` syntax to
12+
// initialize into the end of a Vec<T>.
13+
14+
#![feature(unsafe_destructor)]
15+
16+
use std::cell::{UnsafeCell};
17+
use std::ops::{Placer,PlacementAgent};
18+
19+
struct EmplaceBack<'a, T:'a> {
20+
vec: &'a mut Vec<T>,
21+
}
22+
23+
pub fn main() {
24+
let mut v : Vec<[f32, ..4]> = vec![];
25+
v.push([10., 20., 30., 40.]);
26+
v.push([11., 21., 31., 41.]);
27+
let mut pv = EmplaceBack { vec: &mut v };
28+
let () = box (pv) [12., 22., 32., 42.];
29+
let v = pv.vec;
30+
assert!(same_contents(
31+
v.as_slice(),
32+
[[10., 20., 30., 40.],
33+
[11., 21., 31., 41.],
34+
[12., 22., 32., 42.],
35+
]));
36+
}
37+
38+
fn same_contents<T:PartialEq>(a: &[[T, ..4]], b: &[[T, ..4]]) -> bool {
39+
assert_eq!(a.len(), b.len());
40+
let len = a.len();
41+
for i in range(0, len) {
42+
if a[i].as_slice() != b[i].as_slice() {
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
49+
struct EmplaceBackAgent<T> {
50+
vec_ptr: *mut Vec<T>,
51+
offset: uint,
52+
}
53+
54+
impl<'a, T> Placer<T, (), EmplaceBackAgent<T>> for EmplaceBack<'a, T> {
55+
fn make_place(&self) -> EmplaceBackAgent<T> {
56+
let len = self.vec.len();
57+
let v = self.vec as *mut Vec<T>;
58+
unsafe {
59+
(*v).reserve_additional(1);
60+
}
61+
EmplaceBackAgent { vec_ptr: v, offset: len }
62+
}
63+
}
64+
65+
impl<T> PlacementAgent<T, ()> for EmplaceBackAgent<T> {
66+
unsafe fn pointer(&self) -> *mut T {
67+
assert_eq!((*self.vec_ptr).len(), self.offset);
68+
assert!(self.offset < (*self.vec_ptr).capacity());
69+
(*self.vec_ptr).as_mut_ptr().offset(self.offset.to_int().unwrap())
70+
}
71+
72+
unsafe fn finalize(self) -> () {
73+
assert_eq!((*self.vec_ptr).len(), self.offset);
74+
assert!(self.offset < (*self.vec_ptr).capacity());
75+
(*self.vec_ptr).set_len(self.offset + 1);
76+
}
77+
}
78+
79+
#[unsafe_destructor]
80+
impl<T> Drop for EmplaceBackAgent<T> {
81+
fn drop(&mut self) {
82+
// Do not need to do anything; all `make_place` did was ensure
83+
// we had some space reserved, it did not touch the state of
84+
// the vector itself.
85+
}
86+
}

0 commit comments

Comments
 (0)