Skip to content

Commit ffd9966

Browse files
committed
auto merge of #15591 : aturon/rust/box-cell-stability, r=alexcrichton
This PR is the outcome of the library stabilization meeting for the `liballoc::owned` and `libcore::cell` modules. Aside from the stability attributes, there are a few breaking changes: * The `owned` modules is now named `boxed`, to better represent its contents. (`box` was unavailable, since it's a keyword.) This will help avoid the misconception that `Box` plays a special role wrt ownership. * The `AnyOwnExt` extension trait is renamed to `BoxAny`, and its `move` method is renamed to `downcast`, in both cases to improve clarity. * The recently-added `AnySendOwnExt` extension trait is removed; it was not being used and is unnecessary. [breaking-change]
2 parents 7a6208f + e0ede9c commit ffd9966

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+112
-107
lines changed

src/liballoc/owned.rs renamed to src/liballoc/boxed.rs

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
1616
use core::default::Default;
1717
use core::fmt;
1818
use core::intrinsics;
19-
use core::kinds::Send;
2019
use core::mem;
2120
use core::option::Option;
2221
use core::raw::TraitObject;
@@ -27,17 +26,19 @@ use core::result::{Ok, Err, Result};
2726
///
2827
/// The following two examples are equivalent:
2928
///
30-
/// use std::owned::HEAP;
29+
/// use std::boxed::HEAP;
3130
///
3231
/// # struct Bar;
3332
/// # impl Bar { fn new(_a: int) { } }
3433
/// let foo = box(HEAP) Bar::new(2);
3534
/// let foo = box Bar::new(2);
36-
#[lang="exchange_heap"]
35+
#[lang = "exchange_heap"]
36+
#[experimental = "may be renamed; uncertain about custom allocator design"]
3737
pub static HEAP: () = ();
3838

3939
/// A type that represents a uniquely-owned value.
40-
#[lang="owned_box"]
40+
#[lang = "owned_box"]
41+
#[unstable = "custom allocators will add an additional type parameter (with default)"]
4142
pub struct Box<T>(*mut T);
4243

4344
impl<T: Default> Default for Box<T> {
@@ -57,7 +58,6 @@ impl<T: Clone> Clone for Box<T> {
5758
}
5859
}
5960

60-
// box pointers
6161
impl<T:PartialEq> PartialEq for Box<T> {
6262
#[inline]
6363
fn eq(&self, other: &Box<T>) -> bool { *(*self) == *(*other) }
@@ -85,48 +85,27 @@ impl<T: Ord> Ord for Box<T> {
8585
impl<T: Eq> Eq for Box<T> {}
8686

8787
/// Extension methods for an owning `Any` trait object
88-
pub trait AnyOwnExt {
88+
#[unstable = "post-DST, the signature of `downcast` will change to take `Box<Self>`"]
89+
pub trait BoxAny {
8990
/// Returns the boxed value if it is of type `T`, or
9091
/// `Err(Self)` if it isn't.
91-
fn move<T: 'static>(self) -> Result<Box<T>, Self>;
92-
}
93-
94-
impl AnyOwnExt for Box<Any> {
95-
#[inline]
96-
fn move<T: 'static>(self) -> Result<Box<T>, Box<Any>> {
97-
if self.is::<T>() {
98-
unsafe {
99-
// Get the raw representation of the trait object
100-
let to: TraitObject =
101-
*mem::transmute::<&Box<Any>, &TraitObject>(&self);
102-
103-
// Prevent destructor on self being run
104-
intrinsics::forget(self);
92+
fn downcast<T: 'static>(self) -> Result<Box<T>, Self>;
10593

106-
// Extract the data pointer
107-
Ok(mem::transmute(to.data))
108-
}
109-
} else {
110-
Err(self)
111-
}
94+
/// Deprecated; this method has been renamed to `downcast`.
95+
#[deprecated = "use downcast instead"]
96+
fn move<T: 'static>(self) -> Result<Box<T>, Self> {
97+
self.downcast::<T>()
11298
}
11399
}
114100

115-
/// Extension methods for an owning `Any+Send` trait object
116-
pub trait AnySendOwnExt {
117-
/// Returns the boxed value if it is of type `T`, or
118-
/// `Err(Self)` if it isn't.
119-
fn move_send<T: 'static>(self) -> Result<Box<T>, Self>;
120-
}
121-
122-
impl AnySendOwnExt for Box<Any+Send> {
101+
impl BoxAny for Box<Any> {
123102
#[inline]
124-
fn move_send<T: 'static>(self) -> Result<Box<T>, Box<Any+Send>> {
103+
fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any>> {
125104
if self.is::<T>() {
126105
unsafe {
127106
// Get the raw representation of the trait object
128107
let to: TraitObject =
129-
*mem::transmute::<&Box<Any+Send>, &TraitObject>(&self);
108+
*mem::transmute::<&Box<Any>, &TraitObject>(&self);
130109

131110
// Prevent destructor on self being run
132111
intrinsics::forget(self);
@@ -166,20 +145,20 @@ mod test {
166145
let a = box 8u as Box<Any>;
167146
let b = box Test as Box<Any>;
168147

169-
match a.move::<uint>() {
148+
match a.downcast::<uint>() {
170149
Ok(a) => { assert!(a == box 8u); }
171150
Err(..) => fail!()
172151
}
173-
match b.move::<Test>() {
152+
match b.downcast::<Test>() {
174153
Ok(a) => { assert!(a == box Test); }
175154
Err(..) => fail!()
176155
}
177156

178157
let a = box 8u as Box<Any>;
179158
let b = box Test as Box<Any>;
180159

181-
assert!(a.move::<Box<Test>>().is_err());
182-
assert!(b.move::<Box<uint>>().is_err());
160+
assert!(a.downcast::<Box<Test>>().is_err());
161+
assert!(b.downcast::<Box<uint>>().is_err());
183162
}
184163

185164
#[test]

src/liballoc/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
//!
2222
//! Currently, there are four major definitions in this library.
2323
//!
24-
//! ## Owned pointers
24+
//! ## Boxed values
2525
//!
26-
//! The [`Box`](owned/index.html) type is the core owned pointer type in rust.
26+
//! The [`Box`](boxed/index.html) type is the core owned pointer type in rust.
2727
//! There can only be one owner of a `Box`, and the owner can decide to mutate
28-
//! the contents.
28+
//! the contents, which live on the heap.
2929
//!
3030
//! This type can be sent among tasks efficiently as the size of a `Box` value
3131
//! is just a pointer. Tree-like data structures are often built on owned
@@ -82,6 +82,12 @@ extern crate libc;
8282
#[cfg(test)] #[phase(plugin, link)] extern crate std;
8383
#[cfg(test)] #[phase(plugin, link)] extern crate log;
8484

85+
// The deprecated name of the boxed module
86+
87+
#[deprecated = "use boxed instead"]
88+
#[cfg(not(test))]
89+
pub use owned = boxed;
90+
8591
// Heaps provided for low-level allocation strategies
8692

8793
pub mod heap;
@@ -91,7 +97,7 @@ pub mod util;
9197
// Primitive types using the heaps above
9298

9399
#[cfg(not(test))]
94-
pub mod owned;
100+
pub mod boxed;
95101
pub mod arc;
96102
pub mod rc;
97103

src/libcollections/btree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
use core::prelude::*;
2222

23-
use alloc::owned::Box;
23+
use alloc::boxed::Box;
2424
use core::fmt;
2525
use core::fmt::Show;
2626

src/libcollections/dlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
use core::prelude::*;
2525

26-
use alloc::owned::Box;
26+
use alloc::boxed::Box;
2727
use core::default::Default;
2828
use core::fmt;
2929
use core::iter;

src/libcollections/hash/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
use core::prelude::*;
6767

68-
use alloc::owned::Box;
68+
use alloc::boxed::Box;
6969
use alloc::rc::Rc;
7070
use core::intrinsics::TypeId;
7171
use core::mem;

src/libcollections/treemap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
use core::prelude::*;
1616

17-
use alloc::owned::Box;
17+
use alloc::boxed::Box;
1818
use core::default::Default;
1919
use core::fmt;
2020
use core::fmt::Show;

src/libcollections/trie.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use core::prelude::*;
1414

15-
use alloc::owned::Box;
15+
use alloc::boxed::Box;
1616
use core::default::Default;
1717
use core::mem::zeroed;
1818
use core::mem;

src/libcore/cell.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,13 @@ use option::{None, Option, Some};
163163
use ty::Unsafe;
164164

165165
/// A mutable memory location that admits only `Copy` data.
166+
#[unstable = "likely to be renamed; otherwise stable"]
166167
pub struct Cell<T> {
167168
value: Unsafe<T>,
168169
noshare: marker::NoShare,
169170
}
170171

172+
#[stable]
171173
impl<T:Copy> Cell<T> {
172174
/// Creates a new `Cell` containing the given value.
173175
pub fn new(value: T) -> Cell<T> {
@@ -192,20 +194,22 @@ impl<T:Copy> Cell<T> {
192194
}
193195
}
194196

195-
#[unstable]
197+
#[unstable = "waiting for `Clone` trait to become stable"]
196198
impl<T:Copy> Clone for Cell<T> {
197199
fn clone(&self) -> Cell<T> {
198200
Cell::new(self.get())
199201
}
200202
}
201203

204+
#[unstable = "waiting for `PartialEq` trait to become stable"]
202205
impl<T:PartialEq + Copy> PartialEq for Cell<T> {
203206
fn eq(&self, other: &Cell<T>) -> bool {
204207
self.get() == other.get()
205208
}
206209
}
207210

208211
/// A mutable memory location with dynamically checked borrow rules
212+
#[unstable = "likely to be renamed; otherwise stable"]
209213
pub struct RefCell<T> {
210214
value: Unsafe<T>,
211215
borrow: Cell<BorrowFlag>,
@@ -221,6 +225,7 @@ static WRITING: BorrowFlag = -1;
221225

222226
impl<T> RefCell<T> {
223227
/// Create a new `RefCell` containing `value`
228+
#[stable]
224229
pub fn new(value: T) -> RefCell<T> {
225230
RefCell {
226231
value: Unsafe::new(value),
@@ -231,6 +236,7 @@ impl<T> RefCell<T> {
231236
}
232237

233238
/// Consumes the `RefCell`, returning the wrapped value.
239+
#[unstable = "may be renamed, depending on global conventions"]
234240
pub fn unwrap(self) -> T {
235241
debug_assert!(self.borrow.get() == UNUSED);
236242
unsafe{self.value.unwrap()}
@@ -242,6 +248,7 @@ impl<T> RefCell<T> {
242248
/// immutable borrows can be taken out at the same time.
243249
///
244250
/// Returns `None` if the value is currently mutably borrowed.
251+
#[unstable = "may be renamed, depending on global conventions"]
245252
pub fn try_borrow<'a>(&'a self) -> Option<Ref<'a, T>> {
246253
match self.borrow.get() {
247254
WRITING => None,
@@ -260,6 +267,7 @@ impl<T> RefCell<T> {
260267
/// # Failure
261268
///
262269
/// Fails if the value is currently mutably borrowed.
270+
#[unstable]
263271
pub fn borrow<'a>(&'a self) -> Ref<'a, T> {
264272
match self.try_borrow() {
265273
Some(ptr) => ptr,
@@ -273,6 +281,7 @@ impl<T> RefCell<T> {
273281
/// cannot be borrowed while this borrow is active.
274282
///
275283
/// Returns `None` if the value is currently borrowed.
284+
#[unstable = "may be renamed, depending on global conventions"]
276285
pub fn try_borrow_mut<'a>(&'a self) -> Option<RefMut<'a, T>> {
277286
match self.borrow.get() {
278287
UNUSED => {
@@ -291,6 +300,7 @@ impl<T> RefCell<T> {
291300
/// # Failure
292301
///
293302
/// Fails if the value is currently borrowed.
303+
#[unstable]
294304
pub fn borrow_mut<'a>(&'a self) -> RefMut<'a, T> {
295305
match self.try_borrow_mut() {
296306
Some(ptr) => ptr,
@@ -299,27 +309,30 @@ impl<T> RefCell<T> {
299309
}
300310
}
301311

302-
#[unstable]
312+
#[unstable = "waiting for `Clone` to become stable"]
303313
impl<T: Clone> Clone for RefCell<T> {
304314
fn clone(&self) -> RefCell<T> {
305315
RefCell::new(self.borrow().clone())
306316
}
307317
}
308318

319+
#[unstable = "waiting for `PartialEq` to become stable"]
309320
impl<T: PartialEq> PartialEq for RefCell<T> {
310321
fn eq(&self, other: &RefCell<T>) -> bool {
311322
*self.borrow() == *other.borrow()
312323
}
313324
}
314325

315326
/// Wraps a borrowed reference to a value in a `RefCell` box.
327+
#[unstable]
316328
pub struct Ref<'b, T> {
317329
// FIXME #12808: strange name to try to avoid interfering with
318330
// field accesses of the contained type via Deref
319331
_parent: &'b RefCell<T>
320332
}
321333

322334
#[unsafe_destructor]
335+
#[unstable]
323336
impl<'b, T> Drop for Ref<'b, T> {
324337
fn drop(&mut self) {
325338
let borrow = self._parent.borrow.get();
@@ -328,6 +341,7 @@ impl<'b, T> Drop for Ref<'b, T> {
328341
}
329342
}
330343

344+
#[unstable = "waiting for `Deref` to become stable"]
331345
impl<'b, T> Deref<T> for Ref<'b, T> {
332346
#[inline]
333347
fn deref<'a>(&'a self) -> &'a T {
@@ -341,7 +355,7 @@ impl<'b, T> Deref<T> for Ref<'b, T> {
341355
///
342356
/// A `Clone` implementation would interfere with the widespread
343357
/// use of `r.borrow().clone()` to clone the contents of a `RefCell`.
344-
#[experimental]
358+
#[experimental = "likely to be moved to a method, pending language changes"]
345359
pub fn clone_ref<'b, T>(orig: &Ref<'b, T>) -> Ref<'b, T> {
346360
// Since this Ref exists, we know the borrow flag
347361
// is not set to WRITING.
@@ -355,13 +369,15 @@ pub fn clone_ref<'b, T>(orig: &Ref<'b, T>) -> Ref<'b, T> {
355369
}
356370

357371
/// Wraps a mutable borrowed reference to a value in a `RefCell` box.
372+
#[unstable]
358373
pub struct RefMut<'b, T> {
359374
// FIXME #12808: strange name to try to avoid interfering with
360375
// field accesses of the contained type via Deref
361376
_parent: &'b RefCell<T>
362377
}
363378

364379
#[unsafe_destructor]
380+
#[unstable]
365381
impl<'b, T> Drop for RefMut<'b, T> {
366382
fn drop(&mut self) {
367383
let borrow = self._parent.borrow.get();
@@ -370,13 +386,15 @@ impl<'b, T> Drop for RefMut<'b, T> {
370386
}
371387
}
372388

389+
#[unstable = "waiting for `Deref` to become stable"]
373390
impl<'b, T> Deref<T> for RefMut<'b, T> {
374391
#[inline]
375392
fn deref<'a>(&'a self) -> &'a T {
376393
unsafe { &*self._parent.value.get() }
377394
}
378395
}
379396

397+
#[unstable = "waiting for `DerefMut` to become stable"]
380398
impl<'b, T> DerefMut<T> for RefMut<'b, T> {
381399
#[inline]
382400
fn deref_mut<'a>(&'a mut self) -> &'a mut T {

src/librustrt/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn clone() -> Option<Vec<Vec<u8>>> { imp::clone() }
4545
mod imp {
4646
use core::prelude::*;
4747

48-
use alloc::owned::Box;
48+
use alloc::boxed::Box;
4949
use collections::vec::Vec;
5050
use core::mem;
5151
use core::slice;

src/librustrt/at_exit_imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
use core::prelude::*;
1616

17-
use alloc::owned::Box;
17+
use alloc::boxed::Box;
1818
use collections::vec::Vec;
1919
use core::atomics;
2020
use core::mem;

src/librustrt/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub use self::unwind::{begin_unwind, begin_unwind_fmt};
3737

3838
use core::prelude::*;
3939

40-
use alloc::owned::Box;
40+
use alloc::boxed::Box;
4141
use core::any::Any;
4242

4343
use task::{Task, BlockedTask, TaskOpts};

0 commit comments

Comments
 (0)