Skip to content

Commit df29b99

Browse files
committed
---
yaml --- r: 132093 b: refs/heads/dist-snap c: eafcf6b h: refs/heads/master i: 132091: 22ae93d v: v3
1 parent b0cdb4d commit df29b99

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
@@ -6,7 +6,7 @@ refs/heads/try: 457a3c991d79b971be07fce75f9d0c12848fb37c
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
9-
refs/heads/dist-snap: 4357da356071e34278c5ef29aa1480360d0659b5
9+
refs/heads/dist-snap: eafcf6ba417973c0d8658b9cfe8c21d809aaa381
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1212
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/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)