Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ptr: split out borrowed pointer utilities #6905

Merged
merged 1 commit into from
Jun 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libextra/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use core::cast;
use core::unstable::sync::UnsafeAtomicRcBox;
use core::ptr;
use core::task;
use core::borrow;

/// As sync::condvar, a mechanism for unlock-and-descheduling and signaling.
pub struct Condvar<'self> {
Expand Down Expand Up @@ -425,7 +426,7 @@ impl<T:Const + Owned> RWARC<T> {
// of this cast is removing the mutability.)
let new_data = cast::transmute_immut(data);
// Downgrade ensured the token belonged to us. Just a sanity check.
assert!(ptr::ref_eq(&(*state).data, new_data));
assert!(borrow::ref_eq(&(*state).data, new_data));
// Produce new token
RWReadMode {
data: new_data,
Expand Down
3 changes: 2 additions & 1 deletion src/libextra/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use core::prelude::*;

use core::borrow;
use core::comm;
use core::ptr;
use core::task;
Expand Down Expand Up @@ -589,7 +590,7 @@ impl RWlock {
/// To be called inside of the write_downgrade block.
pub fn downgrade<'a>(&self, token: RWlockWriteMode<'a>)
-> RWlockReadMode<'a> {
if !ptr::ref_eq(self, token.lock) {
if !borrow::ref_eq(self, token.lock) {
fail!("Can't downgrade() with a different rwlock's write_mode!");
}
unsafe {
Expand Down
60 changes: 60 additions & 0 deletions src/libstd/borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Borrowed pointer utilities

#[cfg(not(test))]
use prelude::*;

/// Cast a region pointer - &T - to a uint.
#[inline(always)]
pub fn to_uint<T>(thing: &T) -> uint {
thing as *T as uint
}

/// Determine if two borrowed pointers point to the same thing.
#[inline(always)]
pub fn ref_eq<'a, 'b, T>(thing: &'a T, other: &'b T) -> bool {
to_uint(thing) == to_uint(other)
}

// Equality for region pointers
#[cfg(not(test))]
impl<'self, T: Eq> Eq for &'self T {
#[inline(always)]
fn eq(&self, other: & &'self T) -> bool {
*(*self) == *(*other)
}
#[inline(always)]
fn ne(&self, other: & &'self T) -> bool {
*(*self) != *(*other)
}
}

// Comparison for region pointers
#[cfg(not(test))]
impl<'self, T: Ord> Ord for &'self T {
#[inline(always)]
fn lt(&self, other: & &'self T) -> bool {
*(*self) < *(*other)
}
#[inline(always)]
fn le(&self, other: & &'self T) -> bool {
*(*self) <= *(*other)
}
#[inline(always)]
fn ge(&self, other: & &'self T) -> bool {
*(*self) >= *(*other)
}
#[inline(always)]
fn gt(&self, other: & &'self T) -> bool {
*(*self) > *(*other)
}
}
1 change: 1 addition & 0 deletions src/libstd/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub mod ascii;
pub mod ptr;
pub mod owned;
pub mod managed;
pub mod borrow;


/* Core language traits */
Expand Down
46 changes: 0 additions & 46 deletions src/libstd/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,6 @@ pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
thing as *mut T
}

/// Cast a region pointer - &T - to a uint.
#[inline(always)]
pub fn to_uint<T>(thing: &T) -> uint {
thing as *T as uint
}

/// Determine if two borrowed pointers point to the same thing.
#[inline(always)]
pub fn ref_eq<'a,'b,T>(thing: &'a T, other: &'b T) -> bool {
to_uint(thing) == to_uint(other)
}

/**
Given a **T (pointer to an array of pointers),
iterate through each *T, up to the provided `len`,
Expand Down Expand Up @@ -411,40 +399,6 @@ impl<T> Ord for *const T {
}
}

// Equality for region pointers
#[cfg(not(test))]
impl<'self,T:Eq> Eq for &'self T {
#[inline(always)]
fn eq(&self, other: & &'self T) -> bool {
*(*self) == *(*other)
}
#[inline(always)]
fn ne(&self, other: & &'self T) -> bool {
*(*self) != *(*other)
}
}

// Comparison for region pointers
#[cfg(not(test))]
impl<'self,T:Ord> Ord for &'self T {
#[inline(always)]
fn lt(&self, other: & &'self T) -> bool {
*(*self) < *(*other)
}
#[inline(always)]
fn le(&self, other: & &'self T) -> bool {
*(*self) <= *(*other)
}
#[inline(always)]
fn ge(&self, other: & &'self T) -> bool {
*(*self) >= *(*other)
}
#[inline(always)]
fn gt(&self, other: & &'self T) -> bool {
*(*self) > *(*other)
}
}

#[cfg(test)]
pub mod ptr_tests {
use super::*;
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/rt/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//! local storage, and logging. Even a 'freestanding' Rust would likely want
//! to implement this.

use borrow;
use cast::transmute;
use libc::{c_void, uintptr_t};
use ptr;
Expand Down Expand Up @@ -64,7 +65,7 @@ impl Task {
// This is just an assertion that `run` was called unsafely
// and this instance of Task is still accessible.
do Local::borrow::<Task> |task| {
assert!(ptr::ref_eq(task, self));
assert!(borrow::ref_eq(task, self));
}

match self.unwinder {
Expand All @@ -89,7 +90,7 @@ impl Task {
// This is just an assertion that `destroy` was called unsafely
// and this instance of Task is still accessible.
do Local::borrow::<Task> |task| {
assert!(ptr::ref_eq(task, self));
assert!(borrow::ref_eq(task, self));
}
match self.storage {
LocalStorage(ptr, Some(ref dtor)) => {
Expand Down
3 changes: 2 additions & 1 deletion src/test/run-pass/borrowck-borrow-from-expr-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::borrow;
use std::ptr;

fn borrow(x: &int, f: &fn(x: &int)) {
Expand All @@ -17,7 +18,7 @@ fn borrow(x: &int, f: &fn(x: &int)) {
fn test1(x: @~int) {
do borrow(&*(*x).clone()) |p| {
let x_a = ptr::to_unsafe_ptr(&**x);
assert!((x_a as uint) != ptr::to_uint(p));
assert!((x_a as uint) != borrow::to_uint(p));
assert_eq!(unsafe{*x_a}, *p);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/cast-region-to-uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::ptr;
use std::borrow;

pub fn main() {
let x = 3;
debug!("&x=%x", ptr::to_uint(&x));
debug!("&x=%x", borrow::to_uint(&x));
}