(index<- ) ./libcore/ptr.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 2014
1 // Copyright 2012-2013 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 // FIXME: talk about offset, copy_memory, copy_nonoverlapping_memory
12
13 //! Conveniences for working with unsafe pointers, the `*T`, and `*mut T` types.
14 //!
15 //! Working with unsafe pointers in Rust is fairly uncommon,
16 //! and often limited to some narrow use cases: holding
17 //! an unsafe pointer when safe pointers are unsuitable;
18 //! checking for null; and converting back to safe pointers.
19 //! As a result, there is not yet an abundance of library code
20 //! for working with unsafe pointers, and in particular,
21 //! since pointer math is fairly uncommon in Rust, it is not
22 //! all that convenient.
23 //!
24 //! Use the [`null` function](fn.null.html) to create null pointers,
25 //! the [`is_null`](trait.RawPtr.html#tymethod.is_null)
26 //! and [`is_not_null`](trait.RawPtr.html#method.is_not_null)
27 //! methods of the [`RawPtr` trait](trait.RawPtr.html) to check for null.
28 //! The `RawPtr` trait is imported by the prelude, so `is_null` etc.
29 //! work everywhere.
30 //!
31 //! # Common ways to create unsafe pointers
32 //!
33 //! ## 1. Coerce a reference (`&T`) or mutable reference (`&mut T`).
34 //!
35 //! ```
36 //! let my_num: int = 10;
37 //! let my_num_ptr: *int = &my_num;
38 //! let mut my_speed: int = 88;
39 //! let my_speed_ptr: *mut int = &mut my_speed;
40 //! ```
41 //!
42 //! This does not take ownership of the original allocation
43 //! and requires no resource management later,
44 //! but you must not use the pointer after its lifetime.
45 //!
46 //! ## 2. Transmute an owned box (`Box<T>`).
47 //!
48 //! The `transmute` function takes, by value, whatever it's given
49 //! and returns it as whatever type is requested, as long as the
50 //! types are the same size. Because `Box<T>` and `*T` have the same
51 //! representation they can be trivially,
52 //! though unsafely, transformed from one type to the other.
53 //!
54 //! ```
55 //! use std::cast;
56 //!
57 //! unsafe {
58 //! let my_num: Box<int> = box 10;
59 //! let my_num: *int = cast::transmute(my_num);
60 //! let my_speed: Box<int> = box 88;
61 //! let my_speed: *mut int = cast::transmute(my_speed);
62 //!
63 //! // By taking ownership of the original `Box<T>` though
64 //! // we are obligated to transmute it back later to be destroyed.
65 //! drop(cast::transmute::<_, Box<int>>(my_speed));
66 //! drop(cast::transmute::<_, Box<int>>(my_num));
67 //! }
68 //! ```
69 //!
70 //! Note that here the call to `drop` is for clarity - it indicates
71 //! that we are done with the given value and it should be destroyed.
72 //!
73 //! ## 3. Get it from C.
74 //!
75 //! ```
76 //! extern crate libc;
77 //!
78 //! use std::mem;
79 //!
80 //! fn main() {
81 //! unsafe {
82 //! let my_num: *mut int = libc::malloc(mem::size_of::<int>() as libc::size_t) as *mut int;
83 //! if my_num.is_null() {
84 //! fail!("failed to allocate memory");
85 //! }
86 //! libc::free(my_num as *mut libc::c_void);
87 //! }
88 //! }
89 //! ```
90 //!
91 //! Usually you wouldn't literally use `malloc` and `free` from Rust,
92 //! but C APIs hand out a lot of pointers generally, so are a common source
93 //! of unsafe pointers in Rust.
94
95 use cast;
96 use clone::Clone;
97 use intrinsics;
98 use iter::{range, Iterator};
99 use mem;
100 use option::{Some, None, Option};
101
102 #[cfg(not(test))] use cmp::{Eq, TotalEq, Ord, Equiv};
103
104 /// Return the offset of the first null pointer in `buf`.
105 #[inline]
106 pub unsafe fn buf_len<T>(buf: **T) -> uint {
107 position(buf, |i| *i == null())
108 }
109
110 impl<T> Clone for *T {
111 #[inline]
112 fn clone(&self) -> *T {
113 *self
114 }
115 }
116
117 impl<T> Clone for *mut T {
118 #[inline]
119 fn clone(&self) -> *mut T {
120 *self
121 }
122 }
123
124 /// Return the first offset `i` such that `f(buf[i]) == true`.
125 #[inline]
126 pub unsafe fn position<T>(buf: *T, f: |&T| -> bool) -> uint {
127 let mut i = 0;
128 loop {
129 if f(&(*buf.offset(i as int))) { return i; }
130 else { i += 1; }
131 }
132 }
133
134 /// Create a null pointer.
135 ///
136 /// # Example
137 ///
138 /// ```
139 /// use std::ptr;
140 ///
141 /// let p: *int = ptr::null();
142 /// assert!(p.is_null());
143 /// ```
144 #[inline]
145 pub fn null<T>() -> *T { 0 as *T }
146
147 /// Create an unsafe mutable null pointer.
148 ///
149 /// # Example
150 ///
151 /// ```
152 /// use std::ptr;
153 ///
154 /// let p: *mut int = ptr::mut_null();
155 /// assert!(p.is_null());
156 /// ```
157 #[inline]
158 pub fn mut_null<T>() -> *mut T { 0 as *mut T }
159
160 /// Copies data from one location to another.
161 ///
162 /// Copies `count` elements (not bytes) from `src` to `dst`. The source
163 /// and destination may overlap.
164 ///
165 /// `copy_memory` is semantically equivalent to C's `memmove`.
166 ///
167 /// # Example
168 ///
169 /// Efficiently create a Rust vector from an unsafe buffer:
170 ///
171 /// ```
172 /// use std::ptr;
173 ///
174 /// unsafe fn from_buf_raw<T>(ptr: *T, elts: uint) -> Vec<T> {
175 /// let mut dst = Vec::with_capacity(elts);
176 /// dst.set_len(elts);
177 /// ptr::copy_memory(dst.as_mut_ptr(), ptr, elts);
178 /// dst
179 /// }
180 /// ```
181 ///
182 #[inline]
183 pub unsafe fn copy_memory<T>(dst: *mut T, src: *T, count: uint) {
184 intrinsics::copy_memory(dst, src, count)
185 }
186
187 /// Copies data from one location to another.
188 ///
189 /// Copies `count` elements (not bytes) from `src` to `dst`. The source
190 /// and destination may *not* overlap.
191 ///
192 /// `copy_nonoverlapping_memory` is semantically equivalent to C's `memcpy`.
193 ///
194 /// # Example
195 ///
196 /// A safe swap function:
197 ///
198 /// ```
199 /// use std::cast;
200 /// use std::mem;
201 /// use std::ptr;
202 ///
203 /// fn swap<T>(x: &mut T, y: &mut T) {
204 /// unsafe {
205 /// // Give ourselves some scratch space to work with
206 /// let mut t: T = mem::uninit();
207 ///
208 /// // Perform the swap, `&mut` pointers never alias
209 /// ptr::copy_nonoverlapping_memory(&mut t, &*x, 1);
210 /// ptr::copy_nonoverlapping_memory(x, &*y, 1);
211 /// ptr::copy_nonoverlapping_memory(y, &t, 1);
212 ///
213 /// // y and t now point to the same thing, but we need to completely forget `tmp`
214 /// // because it's no longer relevant.
215 /// cast::forget(t);
216 /// }
217 /// }
218 /// ```
219 ///
220 /// # Safety Note
221 ///
222 /// If the source and destination overlap then the behavior of this
223 /// function is undefined.
224 #[inline]
225 pub unsafe fn copy_nonoverlapping_memory<T>(dst: *mut T,
226 src: *T,
227 count: uint) {
228 intrinsics::copy_nonoverlapping_memory(dst, src, count)
229 }
230
231 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
232 /// bytes of memory starting at `dst` to `c`.
233 #[inline]
234 pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) {
235 intrinsics::set_memory(dst, c, count)
236 }
237
238 /// Zeroes out `count * size_of::<T>` bytes of memory at `dst`
239 #[inline]
240 pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
241 set_memory(dst, 0, count);
242 }
243
244 /// Swap the values at two mutable locations of the same type, without
245 /// deinitialising either. They may overlap.
246 #[inline]
247 pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
248 // Give ourselves some scratch space to work with
249 let mut tmp: T = mem::uninit();
250 let t: *mut T = &mut tmp;
251
252 // Perform the swap
253 copy_nonoverlapping_memory(t, &*x, 1);
254 copy_memory(x, &*y, 1); // `x` and `y` may overlap
255 copy_nonoverlapping_memory(y, &*t, 1);
256
257 // y and t now point to the same thing, but we need to completely forget `tmp`
258 // because it's no longer relevant.
259 cast::forget(tmp);
260 }
261
262 /// Replace the value at a mutable location with a new one, returning the old
263 /// value, without deinitialising either.
264 #[inline]
265 pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
266 mem::swap(cast::transmute(dest), &mut src); // cannot overlap
267 src
268 }
269
270 /// Reads the value from `*src` and returns it.
271 #[inline(always)]
272 pub unsafe fn read<T>(src: *T) -> T {
273 let mut tmp: T = mem::uninit();
274 copy_nonoverlapping_memory(&mut tmp, src, 1);
275 tmp
276 }
277
278 /// Reads the value from `*src` and nulls it out.
279 /// This currently prevents destructors from executing.
280 #[inline(always)]
281 pub unsafe fn read_and_zero<T>(dest: *mut T) -> T {
282 // Copy the data out from `dest`:
283 let tmp = read(&*dest);
284
285 // Now zero out `dest`:
286 zero_memory(dest, 1);
287
288 tmp
289 }
290
291 /// Given a **T (pointer to an array of pointers),
292 /// iterate through each *T, up to the provided `len`,
293 /// passing to the provided callback function
294 pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) {
295 if arr.is_null() {
296 fail!("ptr::array_each_with_len failure: arr input is null pointer");
297 }
298 //let start_ptr = *arr;
299 for e in range(0, len) {
300 let n = arr.offset(e as int);
301 cb(*n);
302 }
303 }
304
305 /// Given a null-pointer-terminated **T (pointer to
306 /// an array of pointers), iterate through each *T,
307 /// passing to the provided callback function
308 ///
309 /// # Safety Note
310 ///
311 /// This will only work with a null-terminated
312 /// pointer array.
313 pub unsafe fn array_each<T>(arr: **T, cb: |*T|) {
314 if arr.is_null() {
315 fail!("ptr::array_each_with_len failure: arr input is null pointer");
316 }
317 let len = buf_len(arr);
318 array_each_with_len(arr, len, cb);
319 }
320
321 /// Extension methods for raw pointers.
322 pub trait RawPtr<T> {
323 /// Returns the null pointer.
324 fn null() -> Self;
325 /// Returns true if the pointer is equal to the null pointer.
326 fn is_null(&self) -> bool;
327 /// Returns true if the pointer is not equal to the null pointer.
328 fn is_not_null(&self) -> bool { !self.is_null() }
329 /// Returns the value of this pointer (ie, the address it points to)
330 fn to_uint(&self) -> uint;
331 /// Returns `None` if the pointer is null, or else returns the value wrapped
332 /// in `Some`.
333 ///
334 /// # Safety Notes
335 ///
336 /// While this method is useful for null-safety, it is important to note
337 /// that this is still an unsafe operation because the returned value could
338 /// be pointing to invalid memory.
339 unsafe fn to_option(&self) -> Option<&T>;
340 /// Calculates the offset from a pointer. The offset *must* be in-bounds of
341 /// the object, or one-byte-past-the-end. `count` is in units of T; e.g. a
342 /// `count` of 3 represents a pointer offset of `3 * sizeof::<T>()` bytes.
343 unsafe fn offset(self, count: int) -> Self;
344 }
345
346 impl<T> RawPtr<T> for *T {
347 #[inline]
348 fn null() -> *T { null() }
349
350 #[inline]
351 fn is_null(&self) -> bool { *self == RawPtr::null() }
352
353 #[inline]
354 fn to_uint(&self) -> uint { *self as uint }
355
356 #[inline]
357 unsafe fn offset(self, count: int) -> *T { intrinsics::offset(self, count) }
358
359 #[inline]
360 unsafe fn to_option(&self) -> Option<&T> {
361 if self.is_null() {
362 None
363 } else {
364 Some(cast::transmute(*self))
365 }
366 }
367 }
368
369 impl<T> RawPtr<T> for *mut T {
370 #[inline]
371 fn null() -> *mut T { mut_null() }
372
373 #[inline]
374 fn is_null(&self) -> bool { *self == RawPtr::null() }
375
376 #[inline]
377 fn to_uint(&self) -> uint { *self as uint }
378
379 #[inline]
380 unsafe fn offset(self, count: int) -> *mut T {
381 intrinsics::offset(self as *T, count) as *mut T
382 }
383
384 #[inline]
385 unsafe fn to_option(&self) -> Option<&T> {
386 if self.is_null() {
387 None
388 } else {
389 Some(cast::transmute(*self))
390 }
391 }
392 }
393
394 // Equality for pointers
395 #[cfg(not(test))]
396 impl<T> Eq for *T {
397 #[inline]
398 fn eq(&self, other: &*T) -> bool {
399 *self == *other
400 }
401 #[inline]
402 fn ne(&self, other: &*T) -> bool { !self.eq(other) }
403 }
404
405 #[cfg(not(test))]
406 impl<T> TotalEq for *T {}
407
408 #[cfg(not(test))]
409 impl<T> Eq for *mut T {
410 #[inline]
411 fn eq(&self, other: &*mut T) -> bool {
412 *self == *other
413 }
414 #[inline]
415 fn ne(&self, other: &*mut T) -> bool { !self.eq(other) }
416 }
417
418 #[cfg(not(test))]
419 impl<T> TotalEq for *mut T {}
420
421 // Equivalence for pointers
422 #[cfg(not(test))]
423 impl<T> Equiv<*mut T> for *T {
424 fn equiv(&self, other: &*mut T) -> bool {
425 self.to_uint() == other.to_uint()
426 }
427 }
428
429 #[cfg(not(test))]
430 impl<T> Equiv<*T> for *mut T {
431 fn equiv(&self, other: &*T) -> bool {
432 self.to_uint() == other.to_uint()
433 }
434 }
435
436 // Equality for extern "C" fn pointers
437 #[cfg(not(test))]
438 mod externfnpointers {
439 use cast;
440 use cmp::Eq;
441
442 impl<_R> Eq for extern "C" fn() -> _R {
443 #[inline]
444 fn eq(&self, other: &extern "C" fn() -> _R) -> bool {
445 let self_: *() = unsafe { cast::transmute(*self) };
446 let other_: *() = unsafe { cast::transmute(*other) };
447 self_ == other_
448 }
449 }
450 macro_rules! fnptreq(
451 ($($p:ident),*) => {
452 impl<_R,$($p),*> Eq for extern "C" fn($($p),*) -> _R {
453 #[inline]
454 fn eq(&self, other: &extern "C" fn($($p),*) -> _R) -> bool {
455 let self_: *() = unsafe { cast::transmute(*self) };
456 let other_: *() = unsafe { cast::transmute(*other) };
457 self_ == other_
458 }
459 }
460 }
461 )
462 fnptreq!(A)
463 fnptreq!(A,B)
464 fnptreq!(A,B,C)
465 fnptreq!(A,B,C,D)
466 fnptreq!(A,B,C,D,E)
467 }
468
469 // Comparison for pointers
470 #[cfg(not(test))]
471 impl<T> Ord for *T {
472 #[inline]
473 fn lt(&self, other: &*T) -> bool { *self < *other }
474 }
475
476 #[cfg(not(test))]
477 impl<T> Ord for *mut T {
478 #[inline]
479 fn lt(&self, other: &*mut T) -> bool { *self < *other }
480 }
481
482 #[cfg(test)]
483 pub mod ptr_tests {
484 use super::*;
485 use realstd::prelude::*;
486
487 use realstd::c_str::ToCStr;
488 use cast;
489 use libc;
490 use realstd::str;
491 use slice::{ImmutableVector, MutableVector};
492
493 #[test]
494 fn test() {
495 unsafe {
496 struct Pair {
497 fst: int,
498 snd: int
499 };
500 let mut p = Pair {fst: 10, snd: 20};
501 let pptr: *mut Pair = &mut p;
502 let iptr: *mut int = cast::transmute(pptr);
503 assert_eq!(*iptr, 10);
504 *iptr = 30;
505 assert_eq!(*iptr, 30);
506 assert_eq!(p.fst, 30);
507
508 *pptr = Pair {fst: 50, snd: 60};
509 assert_eq!(*iptr, 50);
510 assert_eq!(p.fst, 50);
511 assert_eq!(p.snd, 60);
512
513 let v0 = box [32000u16, 32001u16, 32002u16];
514 let mut v1 = box [0u16, 0u16, 0u16];
515
516 copy_memory(v1.as_mut_ptr().offset(1),
517 v0.as_ptr().offset(1), 1);
518 assert!((v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16));
519 copy_memory(v1.as_mut_ptr(),
520 v0.as_ptr().offset(2), 1);
521 assert!((v1[0] == 32002u16 && v1[1] == 32001u16 &&
522 v1[2] == 0u16));
523 copy_memory(v1.as_mut_ptr().offset(2),
524 v0.as_ptr(), 1u);
525 assert!((v1[0] == 32002u16 && v1[1] == 32001u16 &&
526 v1[2] == 32000u16));
527 }
528 }
529
530 #[test]
531 fn test_position() {
532 use libc::c_char;
533
534 "hello".with_c_str(|p| {
535 unsafe {
536 assert!(2u == position(p, |c| *c == 'l' as c_char));
537 assert!(4u == position(p, |c| *c == 'o' as c_char));
538 assert!(5u == position(p, |c| *c == 0 as c_char));
539 }
540 })
541 }
542
543 #[test]
544 fn test_buf_len() {
545 "hello".with_c_str(|p0| {
546 "there".with_c_str(|p1| {
547 "thing".with_c_str(|p2| {
548 let v = box [p0, p1, p2, null()];
549 unsafe {
550 assert_eq!(buf_len(v.as_ptr()), 3u);
551 }
552 })
553 })
554 })
555 }
556
557 #[test]
558 fn test_is_null() {
559 let p: *int = null();
560 assert!(p.is_null());
561 assert!(!p.is_not_null());
562
563 let q = unsafe { p.offset(1) };
564 assert!(!q.is_null());
565 assert!(q.is_not_null());
566
567 let mp: *mut int = mut_null();
568 assert!(mp.is_null());
569 assert!(!mp.is_not_null());
570
571 let mq = unsafe { mp.offset(1) };
572 assert!(!mq.is_null());
573 assert!(mq.is_not_null());
574 }
575
576 #[test]
577 fn test_to_option() {
578 unsafe {
579 let p: *int = null();
580 assert_eq!(p.to_option(), None);
581
582 let q: *int = &2;
583 assert_eq!(q.to_option().unwrap(), &2);
584
585 let p: *mut int = mut_null();
586 assert_eq!(p.to_option(), None);
587
588 let q: *mut int = &mut 2;
589 assert_eq!(q.to_option().unwrap(), &2);
590 }
591 }
592
593 #[test]
594 fn test_ptr_addition() {
595 unsafe {
596 let xs = box [5, ..16];
597 let mut ptr = xs.as_ptr();
598 let end = ptr.offset(16);
599
600 while ptr < end {
601 assert_eq!(*ptr, 5);
602 ptr = ptr.offset(1);
603 }
604
605 let mut xs_mut = xs.clone();
606 let mut m_ptr = xs_mut.as_mut_ptr();
607 let m_end = m_ptr.offset(16);
608
609 while m_ptr < m_end {
610 *m_ptr += 5;
611 m_ptr = m_ptr.offset(1);
612 }
613
614 assert_eq!(xs_mut, box [10, ..16]);
615 }
616 }
617
618 #[test]
619 fn test_ptr_subtraction() {
620 unsafe {
621 let xs = box [0,1,2,3,4,5,6,7,8,9];
622 let mut idx = 9i8;
623 let ptr = xs.as_ptr();
624
625 while idx >= 0i8 {
626 assert_eq!(*(ptr.offset(idx as int)), idx as int);
627 idx = idx - 1i8;
628 }
629
630 let mut xs_mut = xs.clone();
631 let m_start = xs_mut.as_mut_ptr();
632 let mut m_ptr = m_start.offset(9);
633
634 while m_ptr >= m_start {
635 *m_ptr += *m_ptr;
636 m_ptr = m_ptr.offset(-1);
637 }
638
639 assert_eq!(xs_mut, box [0,2,4,6,8,10,12,14,16,18]);
640 }
641 }
642
643 #[test]
644 fn test_ptr_array_each_with_len() {
645 unsafe {
646 let one = "oneOne".to_c_str();
647 let two = "twoTwo".to_c_str();
648 let three = "threeThree".to_c_str();
649 let arr = box [
650 one.with_ref(|buf| buf),
651 two.with_ref(|buf| buf),
652 three.with_ref(|buf| buf),
653 ];
654 let expected_arr = [
655 one, two, three
656 ];
657
658 let mut ctr = 0;
659 let mut iteration_count = 0;
660 array_each_with_len(arr.as_ptr(), arr.len(), |e| {
661 let actual = str::raw::from_c_str(e);
662 let expected = expected_arr[ctr].with_ref(|buf| {
663 str::raw::from_c_str(buf)
664 });
665 debug!(
666 "test_ptr_array_each_with_len e: {}, a: {}",
667 expected, actual);
668 assert_eq!(actual, expected);
669 ctr += 1;
670 iteration_count += 1;
671 });
672 assert_eq!(iteration_count, 3u);
673 }
674 }
675
676 #[test]
677 fn test_ptr_array_each() {
678 unsafe {
679 let one = "oneOne".to_c_str();
680 let two = "twoTwo".to_c_str();
681 let three = "threeThree".to_c_str();
682 let arr = box [
683 one.with_ref(|buf| buf),
684 two.with_ref(|buf| buf),
685 three.with_ref(|buf| buf),
686 // fake a null terminator
687 null(),
688 ];
689 let expected_arr = [
690 one, two, three
691 ];
692
693 let arr_ptr = arr.as_ptr();
694 let mut ctr = 0;
695 let mut iteration_count = 0;
696 array_each(arr_ptr, |e| {
697 let actual = str::raw::from_c_str(e);
698 let expected = expected_arr[ctr].with_ref(|buf| {
699 str::raw::from_c_str(buf)
700 });
701 debug!(
702 "test_ptr_array_each e: {}, a: {}",
703 expected, actual);
704 assert_eq!(actual, expected);
705 ctr += 1;
706 iteration_count += 1;
707 });
708 assert_eq!(iteration_count, 3);
709 }
710 }
711
712 #[test]
713 #[should_fail]
714 fn test_ptr_array_each_with_len_null_ptr() {
715 unsafe {
716 array_each_with_len(0 as **libc::c_char, 1, |e| {
717 str::raw::from_c_str(e);
718 });
719 }
720 }
721 #[test]
722 #[should_fail]
723 fn test_ptr_array_each_null_ptr() {
724 unsafe {
725 array_each(0 as **libc::c_char, |e| {
726 str::raw::from_c_str(e);
727 });
728 }
729 }
730
731 #[test]
732 fn test_set_memory() {
733 let mut xs = [0u8, ..20];
734 let ptr = xs.as_mut_ptr();
735 unsafe { set_memory(ptr, 5u8, xs.len()); }
736 assert!(xs == [5u8, ..20]);
737 }
738 }
libcore/ptr.rs:271:18-271:18 -fn- definition:
pub unsafe fn read<T>(src: *T) -> T {
let mut tmp: T = mem::uninit();
copy_nonoverlapping_memory(&mut tmp, src, 1);
references:- 2libcore/should_not_exist.rs:
172: for j in range(0, *i as int) {
173: ptr::read(&*p.offset(j));
174: }
libcore/ptr.rs:
282: // Copy the data out from `dest`:
283: let tmp = read(&*dest);
libcore/ptr.rs:144:10-144:10 -fn- definition:
pub fn null<T>() -> *T { 0 as *T }
/// Create an unsafe mutable null pointer.
///
references:- 2106: pub unsafe fn buf_len<T>(buf: **T) -> uint {
107: position(buf, |i| *i == null())
108: }
--
347: #[inline]
348: fn null() -> *T { null() }
libcore/ptr.rs:321:40-321:40 -trait- definition:
/// Extension methods for raw pointers.
pub trait RawPtr<T> {
/// Returns the null pointer.
references:- 4346: impl<T> RawPtr<T> for *T {
347: #[inline]
--
369: impl<T> RawPtr<T> for *mut T {
370: #[inline]
libcore/ptr.rs:233:10-233:10 -fn- definition:
pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) {
intrinsics::set_memory(dst, c, count)
}
references:- 2libcore/slice.rs:
1240: fn set_memory(self, value: u8) {
1241: unsafe { ptr::set_memory(self.as_mut_ptr(), value, self.len()) };
1242: }
libcore/ptr.rs:
240: pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
241: set_memory(dst, 0, count);
242: }
libcore/ptr.rs:224:10-224:10 -fn- definition:
pub unsafe fn copy_nonoverlapping_memory<T>(dst: *mut T,
src: *T,
count: uint) {
references:- 12254: copy_memory(x, &*y, 1); // `x` and `y` may overlap
255: copy_nonoverlapping_memory(y, &*t, 1);
--
273: let mut tmp: T = mem::uninit();
274: copy_nonoverlapping_memory(&mut tmp, src, 1);
275: tmp
libcore/slice.rs:
1124: assert!(self.len() >= len_src);
1125: ptr::copy_nonoverlapping_memory(self.as_mut_ptr(), src.as_ptr(), len_src)
1126: }
libcore/should_not_exist.rs:
130: let base = &mut (*ptr).data as *mut _;
131: ptr::copy_nonoverlapping_memory(base,
132: self.as_bytes().as_ptr(),
--
134: let base = base.offset(self.len() as int);
135: ptr::copy_nonoverlapping_memory(base,
136: rhs.as_bytes().as_ptr(),
libcore/cast.rs:
49: let src_ptr: *u8 = transmute(src);
50: copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>());
51: dest
libcore/mem.rs:
235: // Perform the swap, `&mut` pointers never alias
236: ptr::copy_nonoverlapping_memory(&mut t, &*x, 1);
237: ptr::copy_nonoverlapping_memory(x, &*y, 1);
238: ptr::copy_nonoverlapping_memory(y, &t, 1);