Skip to content

Commit 0499033

Browse files
committed
---
yaml --- r: 125789 b: refs/heads/try c: eafcf6b h: refs/heads/master i: 125787: a717825 v: v3
1 parent 116d491 commit 0499033

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: f2fa55903e378368ed9173560f03a0ef16e371c2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 9fc8394d3bce22ab483f98842434c84c396212ae
5-
refs/heads/try: 4357da356071e34278c5ef29aa1480360d0659b5
5+
refs/heads/try: eafcf6ba417973c0d8658b9cfe8c21d809aaa381
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libcollections/vec.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,37 @@ impl<T> Vec<T> {
204204
/// - there must be `length` valid instances of type `T` at the
205205
/// beginning of that allocation
206206
/// - `ptr` must be allocated by the default `Vec` allocator
207+
///
208+
/// # Example
209+
///
210+
/// ```
211+
/// use std::ptr;
212+
/// use std::mem;
213+
///
214+
/// fn main() {
215+
/// let mut v = vec![1i, 2, 3];
216+
///
217+
/// // Pull out the various important pieces of information about `v`
218+
/// let p = v.as_mut_ptr();
219+
/// let len = v.len();
220+
/// let cap = v.capacity();
221+
///
222+
/// unsafe {
223+
/// // Cast `v` into the void: no destructor run, so we are in
224+
/// // complete control of the allocation to which `p` points.
225+
/// mem::forget(v);
226+
///
227+
/// // Overwrite memory with 4, 5, 6
228+
/// for i in range(0, len as int) {
229+
/// ptr::write(p.offset(i), 4 + i);
230+
/// }
231+
///
232+
/// // Put everything back together into a Vec
233+
/// let rebuilt = Vec::from_raw_parts(len, cap, p);
234+
/// assert_eq!(rebuilt, vec![4i, 5i, 6i]);
235+
/// }
236+
/// }
237+
/// ```
207238
pub unsafe fn from_raw_parts(length: uint, capacity: uint,
208239
ptr: *mut T) -> Vec<T> {
209240
Vec { len: length, cap: capacity, ptr: ptr }
@@ -1312,13 +1343,13 @@ impl<T> Vec<T> {
13121343
/// # Example
13131344
///
13141345
/// ```
1315-
/// use std::vec::raw;
1316-
///
13171346
/// let v = vec![1i, 2, 3];
13181347
/// let p = v.as_ptr();
13191348
/// unsafe {
1320-
/// let b = raw::from_buf(p, 3u);
1321-
/// assert_eq!(b, vec![1i, 2, 3]);
1349+
/// // Examine each element manually
1350+
/// assert_eq!(*p, 1i);
1351+
/// assert_eq!(*p.offset(1), 2i);
1352+
/// assert_eq!(*p.offset(2), 3i);
13221353
/// }
13231354
/// ```
13241355
#[inline]
@@ -1343,8 +1374,9 @@ impl<T> Vec<T> {
13431374
/// let p = v.as_mut_ptr();
13441375
/// unsafe {
13451376
/// ptr::write(p, 9i);
1377+
/// ptr::write(p.offset(2), 5i);
13461378
/// }
1347-
/// assert_eq!(v, vec![9i, 2, 3]);
1379+
/// assert_eq!(v, vec![9i, 2, 5]);
13481380
/// ```
13491381
#[inline]
13501382
pub fn as_mut_ptr(&mut self) -> *mut T {

0 commit comments

Comments
 (0)