Skip to content

Commit c40baf6

Browse files
committed
auto merge of #6905 : thestinger/rust/ptr, r=catamorphism
The ptr module is intended to be for raw pointers. Closes #3111
2 parents dad9456 + 4541331 commit c40baf6

File tree

8 files changed

+72
-53
lines changed

8 files changed

+72
-53
lines changed

src/libextra/arc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use core::cast;
4848
use core::unstable::sync::UnsafeAtomicRcBox;
4949
use core::ptr;
5050
use core::task;
51+
use core::borrow;
5152

5253
/// As sync::condvar, a mechanism for unlock-and-descheduling and signaling.
5354
pub struct Condvar<'self> {
@@ -425,7 +426,7 @@ impl<T:Const + Owned> RWARC<T> {
425426
// of this cast is removing the mutability.)
426427
let new_data = cast::transmute_immut(data);
427428
// Downgrade ensured the token belonged to us. Just a sanity check.
428-
assert!(ptr::ref_eq(&(*state).data, new_data));
429+
assert!(borrow::ref_eq(&(*state).data, new_data));
429430
// Produce new token
430431
RWReadMode {
431432
data: new_data,

src/libextra/sync.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use core::prelude::*;
1919

20+
use core::borrow;
2021
use core::comm;
2122
use core::ptr;
2223
use core::task;
@@ -589,7 +590,7 @@ impl RWlock {
589590
/// To be called inside of the write_downgrade block.
590591
pub fn downgrade<'a>(&self, token: RWlockWriteMode<'a>)
591592
-> RWlockReadMode<'a> {
592-
if !ptr::ref_eq(self, token.lock) {
593+
if !borrow::ref_eq(self, token.lock) {
593594
fail!("Can't downgrade() with a different rwlock's write_mode!");
594595
}
595596
unsafe {

src/libstd/borrow.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
//! Borrowed pointer utilities
12+
13+
#[cfg(not(test))]
14+
use prelude::*;
15+
16+
/// Cast a region pointer - &T - to a uint.
17+
#[inline(always)]
18+
pub fn to_uint<T>(thing: &T) -> uint {
19+
thing as *T as uint
20+
}
21+
22+
/// Determine if two borrowed pointers point to the same thing.
23+
#[inline(always)]
24+
pub fn ref_eq<'a, 'b, T>(thing: &'a T, other: &'b T) -> bool {
25+
to_uint(thing) == to_uint(other)
26+
}
27+
28+
// Equality for region pointers
29+
#[cfg(not(test))]
30+
impl<'self, T: Eq> Eq for &'self T {
31+
#[inline(always)]
32+
fn eq(&self, other: & &'self T) -> bool {
33+
*(*self) == *(*other)
34+
}
35+
#[inline(always)]
36+
fn ne(&self, other: & &'self T) -> bool {
37+
*(*self) != *(*other)
38+
}
39+
}
40+
41+
// Comparison for region pointers
42+
#[cfg(not(test))]
43+
impl<'self, T: Ord> Ord for &'self T {
44+
#[inline(always)]
45+
fn lt(&self, other: & &'self T) -> bool {
46+
*(*self) < *(*other)
47+
}
48+
#[inline(always)]
49+
fn le(&self, other: & &'self T) -> bool {
50+
*(*self) <= *(*other)
51+
}
52+
#[inline(always)]
53+
fn ge(&self, other: & &'self T) -> bool {
54+
*(*self) >= *(*other)
55+
}
56+
#[inline(always)]
57+
fn gt(&self, other: & &'self T) -> bool {
58+
*(*self) > *(*other)
59+
}
60+
}

src/libstd/core.rc

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub mod ascii;
125125
pub mod ptr;
126126
pub mod owned;
127127
pub mod managed;
128+
pub mod borrow;
128129

129130

130131
/* Core language traits */

src/libstd/ptr.rs

-46
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,6 @@ pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
255255
thing as *mut T
256256
}
257257

258-
/// Cast a region pointer - &T - to a uint.
259-
#[inline(always)]
260-
pub fn to_uint<T>(thing: &T) -> uint {
261-
thing as *T as uint
262-
}
263-
264-
/// Determine if two borrowed pointers point to the same thing.
265-
#[inline(always)]
266-
pub fn ref_eq<'a,'b,T>(thing: &'a T, other: &'b T) -> bool {
267-
to_uint(thing) == to_uint(other)
268-
}
269-
270258
/**
271259
Given a **T (pointer to an array of pointers),
272260
iterate through each *T, up to the provided `len`,
@@ -411,40 +399,6 @@ impl<T> Ord for *const T {
411399
}
412400
}
413401

414-
// Equality for region pointers
415-
#[cfg(not(test))]
416-
impl<'self,T:Eq> Eq for &'self T {
417-
#[inline(always)]
418-
fn eq(&self, other: & &'self T) -> bool {
419-
*(*self) == *(*other)
420-
}
421-
#[inline(always)]
422-
fn ne(&self, other: & &'self T) -> bool {
423-
*(*self) != *(*other)
424-
}
425-
}
426-
427-
// Comparison for region pointers
428-
#[cfg(not(test))]
429-
impl<'self,T:Ord> Ord for &'self T {
430-
#[inline(always)]
431-
fn lt(&self, other: & &'self T) -> bool {
432-
*(*self) < *(*other)
433-
}
434-
#[inline(always)]
435-
fn le(&self, other: & &'self T) -> bool {
436-
*(*self) <= *(*other)
437-
}
438-
#[inline(always)]
439-
fn ge(&self, other: & &'self T) -> bool {
440-
*(*self) >= *(*other)
441-
}
442-
#[inline(always)]
443-
fn gt(&self, other: & &'self T) -> bool {
444-
*(*self) > *(*other)
445-
}
446-
}
447-
448402
#[cfg(test)]
449403
pub mod ptr_tests {
450404
use super::*;

src/libstd/rt/task.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! local storage, and logging. Even a 'freestanding' Rust would likely want
1414
//! to implement this.
1515
16+
use borrow;
1617
use cast::transmute;
1718
use libc::{c_void, uintptr_t};
1819
use ptr;
@@ -64,7 +65,7 @@ impl Task {
6465
// This is just an assertion that `run` was called unsafely
6566
// and this instance of Task is still accessible.
6667
do Local::borrow::<Task> |task| {
67-
assert!(ptr::ref_eq(task, self));
68+
assert!(borrow::ref_eq(task, self));
6869
}
6970

7071
match self.unwinder {
@@ -89,7 +90,7 @@ impl Task {
8990
// This is just an assertion that `destroy` was called unsafely
9091
// and this instance of Task is still accessible.
9192
do Local::borrow::<Task> |task| {
92-
assert!(ptr::ref_eq(task, self));
93+
assert!(borrow::ref_eq(task, self));
9394
}
9495
match self.storage {
9596
LocalStorage(ptr, Some(ref dtor)) => {

src/test/run-pass/borrowck-borrow-from-expr-block.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::borrow;
1112
use std::ptr;
1213

1314
fn borrow(x: &int, f: &fn(x: &int)) {
@@ -17,7 +18,7 @@ fn borrow(x: &int, f: &fn(x: &int)) {
1718
fn test1(x: @~int) {
1819
do borrow(&*(*x).clone()) |p| {
1920
let x_a = ptr::to_unsafe_ptr(&**x);
20-
assert!((x_a as uint) != ptr::to_uint(p));
21+
assert!((x_a as uint) != borrow::to_uint(p));
2122
assert_eq!(unsafe{*x_a}, *p);
2223
}
2324
}

src/test/run-pass/cast-region-to-uint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::ptr;
11+
use std::borrow;
1212

1313
pub fn main() {
1414
let x = 3;
15-
debug!("&x=%x", ptr::to_uint(&x));
15+
debug!("&x=%x", borrow::to_uint(&x));
1616
}

0 commit comments

Comments
 (0)