libcore: De-export box, cast, and dlist

This commit is contained in:
Patrick Walton 2012-09-26 15:24:31 -07:00
parent e7b8388cb4
commit f41cf208b7
4 changed files with 44 additions and 54 deletions

View File

@ -7,26 +7,22 @@
use cmp::{Eq, Ord};
use intrinsic::TyDesc;
export ptr_eq, raw;
mod raw {
#[legacy_exports];
struct BoxHeaderRepr {
pub mod raw {
pub struct BoxHeaderRepr {
ref_count: uint,
type_desc: *TyDesc,
prev: *BoxRepr,
next: *BoxRepr,
}
struct BoxRepr {
pub struct BoxRepr {
header: BoxHeaderRepr,
data: u8
}
}
pure fn ptr_eq<T>(a: @T, b: @T) -> bool {
pub pure fn ptr_eq<T>(a: @T, b: @T) -> bool {
//! Determine if two shared boxes point to the same object
unsafe { ptr::addr_of(*a) == ptr::addr_of(*b) }
}

View File

@ -1,11 +1,5 @@
//! Unsafe operations
export reinterpret_cast, forget, bump_box_refcount, transmute;
export transmute_mut, transmute_immut, transmute_region, transmute_mut_region;
export transmute_mut_unsafe, transmute_immut_unsafe;
export copy_lifetime, copy_lifetime_vec;
#[abi = "rust-intrinsic"]
extern mod rusti {
#[legacy_exports];
@ -15,7 +9,7 @@ extern mod rusti {
/// Casts the value at `src` to U. The two types must have the same length.
#[inline(always)]
unsafe fn reinterpret_cast<T, U>(src: &T) -> U {
pub unsafe fn reinterpret_cast<T, U>(src: &T) -> U {
rusti::reinterpret_cast(*src)
}
@ -28,7 +22,7 @@ unsafe fn reinterpret_cast<T, U>(src: &T) -> U {
* reinterpret_cast on managed pointer types.
*/
#[inline(always)]
unsafe fn forget<T>(-thing: T) { rusti::forget(move thing); }
pub unsafe fn forget<T>(-thing: T) { rusti::forget(move thing); }
/**
* Force-increment the reference count on a shared box. If used
@ -36,7 +30,7 @@ unsafe fn forget<T>(-thing: T) { rusti::forget(move thing); }
* and/or reinterpret_cast when such calls would otherwise scramble a box's
* reference count
*/
unsafe fn bump_box_refcount<T>(+t: @T) { forget(move t); }
pub unsafe fn bump_box_refcount<T>(+t: @T) { forget(move t); }
/**
* Transform a value of one type into a value of another type.
@ -47,7 +41,7 @@ unsafe fn bump_box_refcount<T>(+t: @T) { forget(move t); }
* assert transmute("L") == ~[76u8, 0u8];
*/
#[inline(always)]
unsafe fn transmute<L, G>(-thing: L) -> G {
pub unsafe fn transmute<L, G>(-thing: L) -> G {
let newthing: G = reinterpret_cast(&thing);
forget(move thing);
move newthing
@ -55,39 +49,45 @@ unsafe fn transmute<L, G>(-thing: L) -> G {
/// Coerce an immutable reference to be mutable.
#[inline(always)]
unsafe fn transmute_mut<T>(+ptr: &a/T) -> &a/mut T { transmute(move ptr) }
pub unsafe fn transmute_mut<T>(+ptr: &a/T) -> &a/mut T { transmute(move ptr) }
/// Coerce a mutable reference to be immutable.
#[inline(always)]
unsafe fn transmute_immut<T>(+ptr: &a/mut T) -> &a/T { transmute(move ptr) }
pub unsafe fn transmute_immut<T>(+ptr: &a/mut T) -> &a/T {
transmute(move ptr)
}
/// Coerce a borrowed pointer to have an arbitrary associated region.
#[inline(always)]
unsafe fn transmute_region<T>(+ptr: &a/T) -> &b/T { transmute(move ptr) }
pub unsafe fn transmute_region<T>(+ptr: &a/T) -> &b/T { transmute(move ptr) }
/// Coerce an immutable reference to be mutable.
#[inline(always)]
unsafe fn transmute_mut_unsafe<T>(+ptr: *const T) -> *mut T { transmute(ptr) }
pub unsafe fn transmute_mut_unsafe<T>(+ptr: *const T) -> *mut T {
transmute(ptr)
}
/// Coerce an immutable reference to be mutable.
#[inline(always)]
unsafe fn transmute_immut_unsafe<T>(+ptr: *const T) -> *T { transmute(ptr) }
pub unsafe fn transmute_immut_unsafe<T>(+ptr: *const T) -> *T {
transmute(ptr)
}
/// Coerce a borrowed mutable pointer to have an arbitrary associated region.
#[inline(always)]
unsafe fn transmute_mut_region<T>(+ptr: &a/mut T) -> &b/mut T {
pub unsafe fn transmute_mut_region<T>(+ptr: &a/mut T) -> &b/mut T {
transmute(move ptr)
}
/// Transforms lifetime of the second pointer to match the first.
#[inline(always)]
unsafe fn copy_lifetime<S,T>(_ptr: &a/S, ptr: &T) -> &a/T {
pub unsafe fn copy_lifetime<S,T>(_ptr: &a/S, ptr: &T) -> &a/T {
transmute_region(ptr)
}
/// Transforms lifetime of the second pointer to match the first.
#[inline(always)]
unsafe fn copy_lifetime_vec<S,T>(_ptr: &a/[S], ptr: &T) -> &a/T {
pub unsafe fn copy_lifetime_vec<S,T>(_ptr: &a/[S], ptr: &T) -> &a/T {
transmute_region(ptr)
}
@ -97,16 +97,14 @@ unsafe fn copy_lifetime_vec<S,T>(_ptr: &a/[S], ptr: &T) -> &a/T {
****************************************************************************/
#[cfg(test)]
mod tests {
#[legacy_exports];
pub mod tests {
#[test]
fn test_reinterpret_cast() {
pub fn test_reinterpret_cast() {
assert 1u == unsafe { reinterpret_cast(&1) };
}
#[test]
fn test_bump_box_refcount() {
pub fn test_bump_box_refcount() {
unsafe {
let box = @~"box box box"; // refcount 1
bump_box_refcount(box); // refcount 2
@ -121,7 +119,7 @@ mod tests {
}
#[test]
fn test_transmute() {
pub fn test_transmute() {
unsafe {
let x = @1;
let x: *int = transmute(x);
@ -131,7 +129,7 @@ mod tests {
}
#[test]
fn test_transmute2() {
pub fn test_transmute2() {
unsafe {
assert ~[76u8, 0u8] == transmute(~"L");
}

View File

@ -14,17 +14,16 @@ and `Eq` to overload the `==` and `!=` operators.
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
use nounittest::*;
use unittest::*;
export Ord;
export Eq;
pub use nounittest::*;
pub use unittest::*;
export Ord, Eq;
/// Interfaces used for comparison.
// Awful hack to work around duplicate lang items in core test.
#[cfg(notest)]
mod nounittest {
#[legacy_exports];
/**
* Trait for values that can be compared for a sort-order.
*
@ -33,7 +32,7 @@ mod nounittest {
* default implementations.
*/
#[lang="ord"]
trait Ord {
pub trait Ord {
pure fn lt(other: &self) -> bool;
pure fn le(other: &self) -> bool;
pure fn ge(other: &self) -> bool;
@ -50,7 +49,7 @@ mod nounittest {
* a default implementation.
*/
#[lang="eq"]
trait Eq {
pub trait Eq {
pure fn eq(other: &self) -> bool;
pure fn ne(other: &self) -> bool;
}
@ -63,14 +62,14 @@ mod nounittest {
#[cfg(test)]
mod unittest {
#[legacy_exports];
trait Ord {
pub trait Ord {
pure fn lt(other: &self) -> bool;
pure fn le(other: &self) -> bool;
pure fn ge(other: &self) -> bool;
pure fn gt(other: &self) -> bool;
}
trait Eq {
pub trait Eq {
pure fn eq(other: &self) -> bool;
pure fn ne(other: &self) -> bool;
}
@ -80,27 +79,27 @@ mod unittest {
mod unittest {
#[legacy_exports];}
pure fn lt<T: Ord>(v1: &T, v2: &T) -> bool {
pub pure fn lt<T: Ord>(v1: &T, v2: &T) -> bool {
(*v1).lt(v2)
}
pure fn le<T: Ord Eq>(v1: &T, v2: &T) -> bool {
pub pure fn le<T: Ord Eq>(v1: &T, v2: &T) -> bool {
(*v1).lt(v2) || (*v1).eq(v2)
}
pure fn eq<T: Eq>(v1: &T, v2: &T) -> bool {
pub pure fn eq<T: Eq>(v1: &T, v2: &T) -> bool {
(*v1).eq(v2)
}
pure fn ne<T: Eq>(v1: &T, v2: &T) -> bool {
pub pure fn ne<T: Eq>(v1: &T, v2: &T) -> bool {
(*v1).ne(v2)
}
pure fn ge<T: Ord>(v1: &T, v2: &T) -> bool {
pub pure fn ge<T: Ord>(v1: &T, v2: &T) -> bool {
(*v1).ge(v2)
}
pure fn gt<T: Ord>(v1: &T, v2: &T) -> bool {
pub pure fn gt<T: Ord>(v1: &T, v2: &T) -> bool {
(*v1).gt(v2)
}

View File

@ -12,9 +12,6 @@ Do not use ==, !=, <, etc on doubly-linked lists -- it may not terminate.
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
export DList;
export new_dlist, from_elem, from_vec, extensions;
type DListLink<T> = Option<DListNode<T>>;
enum DListNode<T> = @{
@ -24,7 +21,7 @@ enum DListNode<T> = @{
mut next: DListLink<T>
};
enum DList<T> {
pub enum DList<T> {
DList_(@{
mut size: uint,
mut hd: DListLink<T>,
@ -94,13 +91,13 @@ pure fn DList<T>() -> DList<T> {
}
/// Creates a new dlist with a single element
pure fn from_elem<T>(+data: T) -> DList<T> {
pub pure fn from_elem<T>(+data: T) -> DList<T> {
let list = DList();
unsafe { list.push(move data); }
list
}
fn from_vec<T: Copy>(+vec: &[T]) -> DList<T> {
pub fn from_vec<T: Copy>(+vec: &[T]) -> DList<T> {
do vec::foldl(DList(), vec) |list,data| {
list.push(data); // Iterating left-to-right -- add newly to the tail.
list