Skip to content

Commit e3c5f69

Browse files
author
boats
committed
Add liballoc APIs.
1 parent 918ef67 commit e3c5f69

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

src/liballoc/boxed.rs

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ use core::cmp::Ordering;
6464
use core::fmt;
6565
use core::hash::{self, Hash, Hasher};
6666
use core::iter::FusedIterator;
67-
use core::marker::{self, Unsize};
68-
use core::mem;
67+
use core::marker::{self, Unpin, Unsize};
68+
use core::mem::{self, Pin};
6969
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
7070
use core::ops::{BoxPlace, Boxed, InPlace, Place, Placer};
7171
use core::ptr::{self, NonNull, Unique};
@@ -896,3 +896,96 @@ impl<T> Generator for Box<T>
896896
(**self).resume()
897897
}
898898
}
899+
900+
/// A pinned, heap allocated reference.
901+
#[unstable(feature = "pin", issue = "0")]
902+
pub struct PinBox<T: ?Sized> {
903+
inner: Box<T>,
904+
}
905+
906+
#[unstable(feature = "pin", issue = "0")]
907+
impl<T> PinBox<T> {
908+
/// Allocate memory on the heap, move the data into it and pin it.
909+
#[unstable(feature = "pin", issue = "0")]
910+
pub fn new(data: T) -> PinBox<T> {
911+
PinBox { inner: Box::new(data) }
912+
}
913+
}
914+
915+
#[unstable(feature = "pin", issue = "0")]
916+
impl<T: ?Sized> PinBox<T> {
917+
/// Get a pinned reference to the data in this PinBox.
918+
pub fn as_pin<'a>(&'a mut self) -> Pin<'a, T> {
919+
unsafe { Pin::new_unchecked(&mut *self.inner) }
920+
}
921+
922+
/// Get a mutable reference to the data inside this PinBox.
923+
///
924+
/// This function is unsafe. Users must guarantee that the data is never
925+
/// moved out of this reference.
926+
pub unsafe fn get_mut<'a>(this: &'a mut PinBox<T>) -> &'a mut T {
927+
&mut *this.inner
928+
}
929+
930+
/// Convert this PinBox into an unpinned Box.
931+
///
932+
/// This function is unsafe. Users must guarantee that the data is never
933+
/// moved out of the box.
934+
pub unsafe fn unpin(this: PinBox<T>) -> Box<T> {
935+
this.inner
936+
}
937+
}
938+
939+
#[unstable(feature = "pin", issue = "0")]
940+
impl<T: ?Sized> From<Box<T>> for PinBox<T> {
941+
fn from(boxed: Box<T>) -> PinBox<T> {
942+
PinBox { inner: boxed }
943+
}
944+
}
945+
946+
#[unstable(feature = "pin", issue = "0")]
947+
impl<T: Unpin + ?Sized> From<PinBox<T>> for Box<T> {
948+
fn from(pinned: PinBox<T>) -> Box<T> {
949+
pinned.inner
950+
}
951+
}
952+
953+
#[unstable(feature = "pin", issue = "0")]
954+
impl<T: ?Sized> Deref for PinBox<T> {
955+
type Target = T;
956+
957+
fn deref(&self) -> &T {
958+
&*self.inner
959+
}
960+
}
961+
962+
#[unstable(feature = "pin", issue = "0")]
963+
impl<T: Unpin + ?Sized> DerefMut for PinBox<T> {
964+
fn deref_mut(&mut self) -> &mut T {
965+
&mut *self.inner
966+
}
967+
}
968+
969+
#[unstable(feature = "pin", issue = "0")]
970+
impl<T: fmt::Display + ?Sized> fmt::Display for PinBox<T> {
971+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
972+
fmt::Display::fmt(&*self.inner, f)
973+
}
974+
}
975+
976+
#[unstable(feature = "pin", issue = "0")]
977+
impl<T: fmt::Debug + ?Sized> fmt::Debug for PinBox<T> {
978+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
979+
fmt::Debug::fmt(&*self.inner, f)
980+
}
981+
}
982+
983+
#[unstable(feature = "pin", issue = "0")]
984+
impl<T: ?Sized> fmt::Pointer for PinBox<T> {
985+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
986+
// It's not possible to extract the inner Uniq directly from the Box,
987+
// instead we cast it to a *const which aliases the Unique
988+
let ptr: *const T = &*self.inner;
989+
fmt::Pointer::fmt(&ptr, f)
990+
}
991+
}

src/liballoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
#![feature(offset_to)]
108108
#![feature(optin_builtin_traits)]
109109
#![feature(pattern)]
110+
#![feature(pin)]
110111
#![feature(placement_in_syntax)]
111112
#![feature(placement_new_protocol)]
112113
#![feature(ptr_internals)]

0 commit comments

Comments
 (0)