1
Fork 0

impl<T> *const T, impl<T> *mut T

This commit is contained in:
Jorge Aparicio 2015-03-10 23:13:36 -05:00
parent 633c593bc3
commit 8afcaabee3
14 changed files with 135 additions and 0 deletions

View file

@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
#[cfg(stage0)]
#[cfg(not(test))] #[cfg(not(test))]
use core::ptr::PtrExt; use core::ptr::PtrExt;

View file

@ -159,7 +159,10 @@ use core::nonzero::NonZero;
use core::ops::{Deref, Drop}; use core::ops::{Deref, Drop};
use core::option::Option; use core::option::Option;
use core::option::Option::{Some, None}; use core::option::Option::{Some, None};
#[cfg(stage0)]
use core::ptr::{self, PtrExt}; use core::ptr::{self, PtrExt};
#[cfg(not(stage0))]
use core::ptr;
use core::result::Result; use core::result::Result;
use core::result::Result::{Ok, Err}; use core::result::Result::{Ok, Err};
use core::intrinsics::assume; use core::intrinsics::assume;

View file

@ -99,6 +99,7 @@ use core::mem;
use core::num::wrapping::WrappingOps; use core::num::wrapping::WrappingOps;
use core::ops::FnMut; use core::ops::FnMut;
use core::option::Option::{self, Some, None}; use core::option::Option::{self, Some, None};
#[cfg(stage0)]
use core::ptr::PtrExt; use core::ptr::PtrExt;
use core::ptr; use core::ptr;
use core::result::Result; use core::result::Result;

View file

@ -42,6 +42,7 @@ pub use iter::{Extend, IteratorExt};
pub use iter::{Iterator, DoubleEndedIterator}; pub use iter::{Iterator, DoubleEndedIterator};
pub use iter::{ExactSizeIterator}; pub use iter::{ExactSizeIterator};
pub use option::Option::{self, Some, None}; pub use option::Option::{self, Some, None};
#[cfg(stage0)]
pub use ptr::{PtrExt, MutPtrExt}; pub use ptr::{PtrExt, MutPtrExt};
pub use result::Result::{self, Ok, Err}; pub use result::Result::{self, Ok, Err};
pub use slice::{AsSlice, SliceExt}; pub use slice::{AsSlice, SliceExt};

View file

@ -262,6 +262,7 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
intrinsics::move_val_init(&mut *dst, src) intrinsics::move_val_init(&mut *dst, src)
} }
#[cfg(stage0)]
/// Methods on raw pointers /// Methods on raw pointers
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub trait PtrExt { pub trait PtrExt {
@ -298,6 +299,7 @@ pub trait PtrExt {
unsafe fn offset(self, count: isize) -> Self where Self::Target: Sized; unsafe fn offset(self, count: isize) -> Self where Self::Target: Sized;
} }
#[cfg(stage0)]
/// Methods on mutable raw pointers /// Methods on mutable raw pointers
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub trait MutPtrExt { pub trait MutPtrExt {
@ -317,6 +319,7 @@ pub trait MutPtrExt {
unsafe fn as_mut<'a>(&self) -> Option<&'a mut Self::Target>; unsafe fn as_mut<'a>(&self) -> Option<&'a mut Self::Target>;
} }
#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> PtrExt for *const T { impl<T: ?Sized> PtrExt for *const T {
type Target = T; type Target = T;
@ -344,6 +347,7 @@ impl<T: ?Sized> PtrExt for *const T {
} }
} }
#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> PtrExt for *mut T { impl<T: ?Sized> PtrExt for *mut T {
type Target = T; type Target = T;
@ -371,6 +375,7 @@ impl<T: ?Sized> PtrExt for *mut T {
} }
} }
#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> MutPtrExt for *mut T { impl<T: ?Sized> MutPtrExt for *mut T {
type Target = T; type Target = T;
@ -388,6 +393,119 @@ impl<T: ?Sized> MutPtrExt for *mut T {
} }
} }
#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
#[lang = "const_ptr"]
impl<T: ?Sized> *const T {
/// Returns true if the pointer is null.
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_null(self) -> bool {
self == 0 as *const T
}
/// Returns `None` if the pointer is null, or else returns a reference to
/// the value wrapped in `Some`.
///
/// # Safety
///
/// While this method and its mutable counterpart are useful for
/// null-safety, it is important to note that this is still an unsafe
/// operation because the returned value could be pointing to invalid
/// memory.
#[unstable(feature = "core",
reason = "Option is not clearly the right return type, and we may want \
to tie the return lifetime to a borrow of the raw pointer")]
#[inline]
pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> {
if self.is_null() {
None
} else {
Some(&**self)
}
}
/// Calculates the offset from a pointer. `count` is in units of T; e.g. a
/// `count` of 3 represents a pointer offset of `3 * sizeof::<T>()` bytes.
///
/// # Safety
///
/// The offset must be in-bounds of the object, or one-byte-past-the-end.
/// Otherwise `offset` invokes Undefined Behaviour, regardless of whether
/// the pointer is used.
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn offset(self, count: isize) -> *const T where T: Sized {
intrinsics::offset(self, count)
}
}
#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
#[lang = "mut_ptr"]
impl<T: ?Sized> *mut T {
/// Returns true if the pointer is null.
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_null(self) -> bool {
self == 0 as *mut T
}
/// Returns `None` if the pointer is null, or else returns a reference to
/// the value wrapped in `Some`.
///
/// # Safety
///
/// While this method and its mutable counterpart are useful for
/// null-safety, it is important to note that this is still an unsafe
/// operation because the returned value could be pointing to invalid
/// memory.
#[unstable(feature = "core",
reason = "Option is not clearly the right return type, and we may want \
to tie the return lifetime to a borrow of the raw pointer")]
#[inline]
pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> {
if self.is_null() {
None
} else {
Some(&**self)
}
}
/// Calculates the offset from a pointer. `count` is in units of T; e.g. a
/// `count` of 3 represents a pointer offset of `3 * sizeof::<T>()` bytes.
///
/// # Safety
///
/// The offset must be in-bounds of the object, or one-byte-past-the-end.
/// Otherwise `offset` invokes Undefined Behaviour, regardless of whether
/// the pointer is used.
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn offset(self, count: isize) -> *mut T where T: Sized {
intrinsics::offset(self, count) as *mut T
}
/// Returns `None` if the pointer is null, or else returns a mutable
/// reference to the value wrapped in `Some`.
///
/// # Safety
///
/// As with `as_ref`, this is unsafe because it cannot verify the validity
/// of the returned pointer.
#[unstable(feature = "core",
reason = "return value does not necessarily convey all possible \
information")]
#[inline]
pub unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> {
if self.is_null() {
None
} else {
Some(&mut **self)
}
}
}
// Equality for pointers // Equality for pointers
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> PartialEq for *const T { impl<T: ?Sized> PartialEq for *const T {

View file

@ -49,6 +49,7 @@ use option::Option::{None, Some};
use result::Result; use result::Result;
use result::Result::{Ok, Err}; use result::Result::{Ok, Err};
use ptr; use ptr;
#[cfg(stage0)]
use ptr::PtrExt; use ptr::PtrExt;
use mem; use mem;
use mem::size_of; use mem::size_of;

View file

@ -30,6 +30,7 @@ use mem;
use num::Int; use num::Int;
use ops::{Fn, FnMut}; use ops::{Fn, FnMut};
use option::Option::{self, None, Some}; use option::Option::{self, None, Some};
#[cfg(stage0)]
use ptr::PtrExt; use ptr::PtrExt;
use raw::{Repr, Slice}; use raw::{Repr, Slice};
use result::Result::{self, Ok, Err}; use result::Result::{self, Ok, Err};

View file

@ -24,7 +24,10 @@ use num::wrapping::{OverflowingOps, WrappingOps};
use ops::{Deref, DerefMut, Drop}; use ops::{Deref, DerefMut, Drop};
use option::Option; use option::Option;
use option::Option::{Some, None}; use option::Option::{Some, None};
#[cfg(stage0)]
use ptr::{self, PtrExt, Unique}; use ptr::{self, PtrExt, Unique};
#[cfg(not(stage0))]
use ptr::{self, Unique};
use rt::heap::{allocate, deallocate, EMPTY}; use rt::heap::{allocate, deallocate, EMPTY};
use collections::hash_state::HashState; use collections::hash_state::HashState;

View file

@ -20,6 +20,7 @@ use iter::Iterator;
use marker::Sized; use marker::Sized;
use ops::{Drop, FnOnce}; use ops::{Drop, FnOnce};
use option::Option::{self, Some, None}; use option::Option::{self, Some, None};
#[cfg(stage0)]
use ptr::PtrExt; use ptr::PtrExt;
use result::Result::{Ok, Err}; use result::Result::{Ok, Err};
use result; use result;

View file

@ -26,6 +26,7 @@ use num::Int;
use ops::FnOnce; use ops::FnOnce;
use option::Option; use option::Option;
use option::Option::{Some, None}; use option::Option::{Some, None};
#[cfg(stage0)]
use ptr::PtrExt; use ptr::PtrExt;
use result::Result::{Ok, Err}; use result::Result::{Ok, Err};
#[cfg(stage0)] #[cfg(stage0)]

View file

@ -935,6 +935,7 @@ impl<'a> Reader for &'a mut (Reader+'a) {
// API yet. If so, it should be a method on Vec. // API yet. If so, it should be a method on Vec.
unsafe fn slice_vec_capacity<'a, T>(v: &'a mut Vec<T>, start: uint, end: uint) -> &'a mut [T] { unsafe fn slice_vec_capacity<'a, T>(v: &'a mut Vec<T>, start: uint, end: uint) -> &'a mut [T] {
use slice; use slice;
#[cfg(stage0)]
use ptr::PtrExt; use ptr::PtrExt;
assert!(start <= end); assert!(start <= end);

View file

@ -52,6 +52,7 @@ use option::Option::{Some, None};
use option::Option; use option::Option;
use old_path::{Path, GenericPath, BytesContainer}; use old_path::{Path, GenericPath, BytesContainer};
use path::{self, PathBuf}; use path::{self, PathBuf};
#[cfg(stage0)]
use ptr::PtrExt; use ptr::PtrExt;
use ptr; use ptr;
use result::Result::{Err, Ok}; use result::Result::{Err, Ok};

View file

@ -40,6 +40,7 @@
#[doc(no_inline)] pub use iter::{Iterator, IteratorExt, Extend}; #[doc(no_inline)] pub use iter::{Iterator, IteratorExt, Extend};
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[doc(no_inline)] pub use option::Option::{self, Some, None}; #[doc(no_inline)] pub use option::Option::{self, Some, None};
#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[doc(no_inline)] pub use ptr::{PtrExt, MutPtrExt}; #[doc(no_inline)] pub use ptr::{PtrExt, MutPtrExt};
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]

View file

@ -12,6 +12,7 @@
//! //!
//! Documentation can be found on the `rt::at_exit` function. //! Documentation can be found on the `rt::at_exit` function.
#[cfg(stage0)]
use core::prelude::*; use core::prelude::*;
use boxed; use boxed;