about summary refs log tree commit diff
path: root/src/libsync
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-08-28 15:01:39 +0000
committerbors <bors@rust-lang.org>2014-08-28 15:01:39 +0000
commitb5165321e48c1fd8422803fb40693afab7939c8c (patch)
treea3206dbc34acc88311a3ec676155240681784906 /src/libsync
parent0d3bd7720c50e3ada4bac77331d43926493be4fe (diff)
parent1b487a890695e7d6dfbfe5dcd7d4fa0e8ca8003f (diff)
downloadrust-b5165321e48c1fd8422803fb40693afab7939c8c.tar.gz
rust-b5165321e48c1fd8422803fb40693afab7939c8c.zip
auto merge of #16453 : nikomatsakis/rust/type-bounds-3, r=pcwalton
Implements https://github.com/rust-lang/rfcs/pull/192.

In particular:

1. type parameters can have lifetime bounds and objects can close over borrowed values, presuming that they have suitable bounds.
2. objects must have a bound, though it may be derived from the trait itself or from a `Send` bound.
3. all types must be well-formed.
4. type parameters and lifetime parameters may themselves have lifetimes as bounds. Something like `T:'a` means "the type T outlives 'a`" and something like `'a:'b`" means "'a outlives 'b". Outlives here means "all borrowed data has a lifetime at least as long".

This is a [breaking-change]. The most common things you have to fix after this change are:

1. Introduce lifetime bounds onto type parameters if your type (directly or indirectly) contains a reference. Thus a struct like `struct Ref<'a, T> { x: &'a T }` would be changed to `struct Ref<'a, T:'a> { x: &'a T }`.
2. Introduce lifetime bounds onto lifetime parameters if your type contains a double reference. Thus a type like `struct RefWrapper<'a, 'b> { r: &'a Ref<'b, int> }` (where `Ref` is defined as before) would need to be changed to `struct RefWrapper<'a, 'b:'a> { ... }`.
2. Explicitly give object lifetimes in structure definitions. Most commonly, this means changing something like `Box<Reader>` to `Box<Reader+'static>`, so as to indicate that this is a reader without any borrowed data. (Note: you may wish to just change to `Box<Reader+Send>` while you're at it; it's a more restrictive type, technically, but means you can send the reader between threads.)

The intuition for points 1 and 2 is that a reference must never outlive its referent (the thing it points at). Therefore, if you have a type `&'a T`, we must know that `T` (whatever it is) outlives `'a`. And so on.

Closes #5723.
Diffstat (limited to 'src/libsync')
-rw-r--r--src/libsync/comm/mod.rs8
-rw-r--r--src/libsync/comm/select.rs20
-rw-r--r--src/libsync/lib.rs1
-rw-r--r--src/libsync/lock.rs34
-rw-r--r--src/libsync/raw.rs7
5 files changed, 69 insertions, 1 deletions
diff --git a/src/libsync/comm/mod.rs b/src/libsync/comm/mod.rs
index 16bcdd9bbb6..16f6eea6144 100644
--- a/src/libsync/comm/mod.rs
+++ b/src/libsync/comm/mod.rs
@@ -386,6 +386,14 @@ pub struct Receiver<T> {
 /// whenever `next` is called, waiting for a new message, and `None` will be
 /// returned when the corresponding channel has hung up.
 #[unstable]
+#[cfg(not(stage0))]
+pub struct Messages<'a, T:'a> {
+    rx: &'a Receiver<T>
+}
+
+/// Stage0 only
+#[cfg(stage0)]
+#[unstable]
 pub struct Messages<'a, T> {
     rx: &'a Receiver<T>
 }
diff --git a/src/libsync/comm/select.rs b/src/libsync/comm/select.rs
index 737a4bfe299..dc9891dd1ee 100644
--- a/src/libsync/comm/select.rs
+++ b/src/libsync/comm/select.rs
@@ -76,6 +76,24 @@ pub struct Select {
 /// A handle to a receiver which is currently a member of a `Select` set of
 /// receivers.  This handle is used to keep the receiver in the set as well as
 /// interact with the underlying receiver.
+#[cfg(not(stage0))]
+pub struct Handle<'rx, T:'rx> {
+    /// The ID of this handle, used to compare against the return value of
+    /// `Select::wait()`
+    id: uint,
+    selector: &'rx Select,
+    next: *mut Handle<'static, ()>,
+    prev: *mut Handle<'static, ()>,
+    added: bool,
+    packet: &'rx Packet+'rx,
+
+    // due to our fun transmutes, we be sure to place this at the end. (nothing
+    // previous relies on T)
+    rx: &'rx Receiver<T>,
+}
+
+/// Stage0 only
+#[cfg(stage0)]
 pub struct Handle<'rx, T> {
     /// The ID of this handle, used to compare against the return value of
     /// `Select::wait()`
@@ -84,7 +102,7 @@ pub struct Handle<'rx, T> {
     next: *mut Handle<'static, ()>,
     prev: *mut Handle<'static, ()>,
     added: bool,
-    packet: &'rx Packet,
+    packet: &'rx Packet+'rx,
 
     // due to our fun transmutes, we be sure to place this at the end. (nothing
     // previous relies on T)
diff --git a/src/libsync/lib.rs b/src/libsync/lib.rs
index bed90743503..c2744751ee5 100644
--- a/src/libsync/lib.rs
+++ b/src/libsync/lib.rs
@@ -29,6 +29,7 @@
 
 #![feature(phase, globs, macro_rules, unsafe_destructor)]
 #![feature(import_shadowing)]
+#![feature(issue_5723_bootstrap)]
 #![deny(missing_doc)]
 #![no_std]
 
diff --git a/src/libsync/lock.rs b/src/libsync/lock.rs
index b07d06ca18e..e1cae6c62d5 100644
--- a/src/libsync/lock.rs
+++ b/src/libsync/lock.rs
@@ -180,6 +180,18 @@ pub struct Mutex<T> {
 
 /// An guard which is created by locking a mutex. Through this guard the
 /// underlying data can be accessed.
+#[cfg(not(stage0))]
+pub struct MutexGuard<'a, T:'a> {
+    // FIXME #12808: strange name to try to avoid interfering with
+    // field accesses of the contained type via Deref
+    _data: &'a mut T,
+    /// Inner condition variable connected to the locked mutex that this guard
+    /// was created from. This can be used for atomic-unlock-and-deschedule.
+    pub cond: Condvar<'a>,
+}
+
+/// stage0 only
+#[cfg(stage0)]
 pub struct MutexGuard<'a, T> {
     // FIXME #12808: strange name to try to avoid interfering with
     // field accesses of the contained type via Deref
@@ -280,6 +292,18 @@ pub struct RWLock<T> {
 
 /// A guard which is created by locking an rwlock in write mode. Through this
 /// guard the underlying data can be accessed.
+#[cfg(not(stage0))]
+pub struct RWLockWriteGuard<'a, T:'a> {
+    // FIXME #12808: strange name to try to avoid interfering with
+    // field accesses of the contained type via Deref
+    _data: &'a mut T,
+    /// Inner condition variable that can be used to sleep on the write mode of
+    /// this rwlock.
+    pub cond: Condvar<'a>,
+}
+
+/// stage0 only
+#[cfg(stage0)]
 pub struct RWLockWriteGuard<'a, T> {
     // FIXME #12808: strange name to try to avoid interfering with
     // field accesses of the contained type via Deref
@@ -291,6 +315,16 @@ pub struct RWLockWriteGuard<'a, T> {
 
 /// A guard which is created by locking an rwlock in read mode. Through this
 /// guard the underlying data can be accessed.
+#[cfg(not(stage0))]
+pub struct RWLockReadGuard<'a, T:'a> {
+    // FIXME #12808: strange names to try to avoid interfering with
+    // field accesses of the contained type via Deref
+    _data: &'a T,
+    _guard: raw::RWLockReadGuard<'a>,
+}
+
+/// Stage0 only
+#[cfg(stage0)]
 pub struct RWLockReadGuard<'a, T> {
     // FIXME #12808: strange names to try to avoid interfering with
     // field accesses of the contained type via Deref
diff --git a/src/libsync/raw.rs b/src/libsync/raw.rs
index c42d567fc18..98934d87474 100644
--- a/src/libsync/raw.rs
+++ b/src/libsync/raw.rs
@@ -103,10 +103,17 @@ struct SemInner<Q> {
 }
 
 #[must_use]
+#[cfg(stage0)]
 struct SemGuard<'a, Q> {
     sem: &'a Sem<Q>,
 }
 
+#[must_use]
+#[cfg(not(stage0))]
+struct SemGuard<'a, Q:'a> {
+    sem: &'a Sem<Q>,
+}
+
 impl<Q: Send> Sem<Q> {
     fn new(count: int, q: Q) -> Sem<Q> {
         assert!(count >= 0,