@@ -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