Skip to content

Commit 380d23b

Browse files
committed
Remove 'static bound from sync::mpsc, Mutex and RwLock.
Adds some basic tests to check that the types still catch the most glaring errors that could occur. cc #22444.
1 parent 522d09d commit 380d23b

12 files changed

+147
-45
lines changed

src/libstd/sync/mpsc/mod.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ pub struct Receiver<T> {
345345

346346
// The receiver port can be sent from place to place, so long as it
347347
// is not used to receive non-sendable things.
348-
unsafe impl<T: Send + 'static> Send for Receiver<T> { }
348+
unsafe impl<T: Send> Send for Receiver<T> { }
349349

350350
/// An iterator over messages on a receiver, this iterator will block
351351
/// whenever `next` is called, waiting for a new message, and `None` will be
@@ -364,7 +364,7 @@ pub struct Sender<T> {
364364

365365
// The send port can be sent from place to place, so long as it
366366
// is not used to send non-sendable things.
367-
unsafe impl<T: Send + 'static> Send for Sender<T> { }
367+
unsafe impl<T: Send> Send for Sender<T> { }
368368

369369
/// The sending-half of Rust's synchronous channel type. This half can only be
370370
/// owned by one task, but it can be cloned to send to other tasks.
@@ -373,7 +373,7 @@ pub struct SyncSender<T> {
373373
inner: Arc<UnsafeCell<sync::Packet<T>>>,
374374
}
375375

376-
unsafe impl<T: Send + 'static> Send for SyncSender<T> {}
376+
unsafe impl<T: Send> Send for SyncSender<T> {}
377377

378378
impl<T> !Sync for SyncSender<T> {}
379379

@@ -485,7 +485,7 @@ impl<T> UnsafeFlavor<T> for Receiver<T> {
485485
/// println!("{:?}", rx.recv().unwrap());
486486
/// ```
487487
#[stable(feature = "rust1", since = "1.0.0")]
488-
pub fn channel<T: Send + 'static>() -> (Sender<T>, Receiver<T>) {
488+
pub fn channel<T: Send>() -> (Sender<T>, Receiver<T>) {
489489
let a = Arc::new(UnsafeCell::new(oneshot::Packet::new()));
490490
(Sender::new(Flavor::Oneshot(a.clone())), Receiver::new(Flavor::Oneshot(a)))
491491
}
@@ -525,7 +525,7 @@ pub fn channel<T: Send + 'static>() -> (Sender<T>, Receiver<T>) {
525525
/// assert_eq!(rx.recv().unwrap(), 2);
526526
/// ```
527527
#[stable(feature = "rust1", since = "1.0.0")]
528-
pub fn sync_channel<T: Send + 'static>(bound: uint) -> (SyncSender<T>, Receiver<T>) {
528+
pub fn sync_channel<T: Send>(bound: uint) -> (SyncSender<T>, Receiver<T>) {
529529
let a = Arc::new(UnsafeCell::new(sync::Packet::new(bound)));
530530
(SyncSender::new(a.clone()), Receiver::new(Flavor::Sync(a)))
531531
}
@@ -534,7 +534,7 @@ pub fn sync_channel<T: Send + 'static>(bound: uint) -> (SyncSender<T>, Receiver<
534534
// Sender
535535
////////////////////////////////////////////////////////////////////////////////
536536

537-
impl<T: Send + 'static> Sender<T> {
537+
impl<T: Send> Sender<T> {
538538
fn new(inner: Flavor<T>) -> Sender<T> {
539539
Sender {
540540
inner: UnsafeCell::new(inner),
@@ -616,7 +616,7 @@ impl<T: Send + 'static> Sender<T> {
616616
}
617617

618618
#[stable(feature = "rust1", since = "1.0.0")]
619-
impl<T: Send + 'static> Clone for Sender<T> {
619+
impl<T: Send> Clone for Sender<T> {
620620
fn clone(&self) -> Sender<T> {
621621
let (packet, sleeper, guard) = match *unsafe { self.inner() } {
622622
Flavor::Oneshot(ref p) => {
@@ -662,7 +662,7 @@ impl<T: Send + 'static> Clone for Sender<T> {
662662

663663
#[unsafe_destructor]
664664
#[stable(feature = "rust1", since = "1.0.0")]
665-
impl<T: Send + 'static> Drop for Sender<T> {
665+
impl<T: Send> Drop for Sender<T> {
666666
fn drop(&mut self) {
667667
match *unsafe { self.inner_mut() } {
668668
Flavor::Oneshot(ref mut p) => unsafe { (*p.get()).drop_chan(); },
@@ -677,7 +677,7 @@ impl<T: Send + 'static> Drop for Sender<T> {
677677
// SyncSender
678678
////////////////////////////////////////////////////////////////////////////////
679679

680-
impl<T: Send + 'static> SyncSender<T> {
680+
impl<T: Send> SyncSender<T> {
681681
fn new(inner: Arc<UnsafeCell<sync::Packet<T>>>) -> SyncSender<T> {
682682
SyncSender { inner: inner }
683683
}
@@ -717,7 +717,7 @@ impl<T: Send + 'static> SyncSender<T> {
717717
}
718718

719719
#[stable(feature = "rust1", since = "1.0.0")]
720-
impl<T: Send + 'static> Clone for SyncSender<T> {
720+
impl<T: Send> Clone for SyncSender<T> {
721721
fn clone(&self) -> SyncSender<T> {
722722
unsafe { (*self.inner.get()).clone_chan(); }
723723
return SyncSender::new(self.inner.clone());
@@ -726,7 +726,7 @@ impl<T: Send + 'static> Clone for SyncSender<T> {
726726

727727
#[unsafe_destructor]
728728
#[stable(feature = "rust1", since = "1.0.0")]
729-
impl<T: Send + 'static> Drop for SyncSender<T> {
729+
impl<T: Send> Drop for SyncSender<T> {
730730
fn drop(&mut self) {
731731
unsafe { (*self.inner.get()).drop_chan(); }
732732
}
@@ -736,7 +736,7 @@ impl<T: Send + 'static> Drop for SyncSender<T> {
736736
// Receiver
737737
////////////////////////////////////////////////////////////////////////////////
738738

739-
impl<T: Send + 'static> Receiver<T> {
739+
impl<T: Send> Receiver<T> {
740740
fn new(inner: Flavor<T>) -> Receiver<T> {
741741
Receiver { inner: UnsafeCell::new(inner) }
742742
}
@@ -855,7 +855,7 @@ impl<T: Send + 'static> Receiver<T> {
855855
}
856856
}
857857

858-
impl<T: Send + 'static> select::Packet for Receiver<T> {
858+
impl<T: Send> select::Packet for Receiver<T> {
859859
fn can_recv(&self) -> bool {
860860
loop {
861861
let new_port = match *unsafe { self.inner() } {
@@ -942,15 +942,15 @@ impl<T: Send + 'static> select::Packet for Receiver<T> {
942942
}
943943

944944
#[stable(feature = "rust1", since = "1.0.0")]
945-
impl<'a, T: Send + 'static> Iterator for Iter<'a, T> {
945+
impl<'a, T: Send> Iterator for Iter<'a, T> {
946946
type Item = T;
947947

948948
fn next(&mut self) -> Option<T> { self.rx.recv().ok() }
949949
}
950950

951951
#[unsafe_destructor]
952952
#[stable(feature = "rust1", since = "1.0.0")]
953-
impl<T: Send + 'static> Drop for Receiver<T> {
953+
impl<T: Send> Drop for Receiver<T> {
954954
fn drop(&mut self) {
955955
match *unsafe { self.inner_mut() } {
956956
Flavor::Oneshot(ref mut p) => unsafe { (*p.get()).drop_port(); },

src/libstd/sync/mpsc/mpsc_queue.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct Queue<T> {
7878
}
7979

8080
unsafe impl<T:Send> Send for Queue<T> { }
81-
unsafe impl<T: Send + 'static> Sync for Queue<T> { }
81+
unsafe impl<T: Send> Sync for Queue<T> { }
8282

8383
impl<T> Node<T> {
8484
unsafe fn new(v: Option<T>) -> *mut Node<T> {
@@ -89,7 +89,7 @@ impl<T> Node<T> {
8989
}
9090
}
9191

92-
impl<T: Send + 'static> Queue<T> {
92+
impl<T: Send> Queue<T> {
9393
/// Creates a new queue that is safe to share among multiple producers and
9494
/// one consumer.
9595
pub fn new() -> Queue<T> {
@@ -140,7 +140,7 @@ impl<T: Send + 'static> Queue<T> {
140140

141141
#[unsafe_destructor]
142142
#[stable(feature = "rust1", since = "1.0.0")]
143-
impl<T: Send + 'static> Drop for Queue<T> {
143+
impl<T: Send> Drop for Queue<T> {
144144
fn drop(&mut self) {
145145
unsafe {
146146
let mut cur = *self.tail.get();

src/libstd/sync/mpsc/oneshot.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ enum MyUpgrade<T> {
8888
GoUp(Receiver<T>),
8989
}
9090

91-
impl<T: Send + 'static> Packet<T> {
91+
impl<T: Send> Packet<T> {
9292
pub fn new() -> Packet<T> {
9393
Packet {
9494
data: None,
@@ -368,7 +368,7 @@ impl<T: Send + 'static> Packet<T> {
368368
}
369369

370370
#[unsafe_destructor]
371-
impl<T: Send + 'static> Drop for Packet<T> {
371+
impl<T: Send> Drop for Packet<T> {
372372
fn drop(&mut self) {
373373
assert_eq!(self.state.load(Ordering::SeqCst), DISCONNECTED);
374374
}

src/libstd/sync/mpsc/select.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl Select {
134134
/// Creates a new handle into this receiver set for a new receiver. Note
135135
/// that this does *not* add the receiver to the receiver set, for that you
136136
/// must call the `add` method on the handle itself.
137-
pub fn handle<'a, T: Send + 'static>(&'a self, rx: &'a Receiver<T>) -> Handle<'a, T> {
137+
pub fn handle<'a, T: Send>(&'a self, rx: &'a Receiver<T>) -> Handle<'a, T> {
138138
let id = self.next_id.get();
139139
self.next_id.set(id + 1);
140140
Handle {
@@ -251,7 +251,7 @@ impl Select {
251251
fn iter(&self) -> Packets { Packets { cur: self.head } }
252252
}
253253

254-
impl<'rx, T: Send + 'static> Handle<'rx, T> {
254+
impl<'rx, T: Send> Handle<'rx, T> {
255255
/// Retrieve the id of this handle.
256256
#[inline]
257257
pub fn id(&self) -> uint { self.id }
@@ -322,7 +322,7 @@ impl Drop for Select {
322322
}
323323

324324
#[unsafe_destructor]
325-
impl<'rx, T: Send + 'static> Drop for Handle<'rx, T> {
325+
impl<'rx, T: Send> Drop for Handle<'rx, T> {
326326
fn drop(&mut self) {
327327
unsafe { self.remove() }
328328
}

src/libstd/sync/mpsc/shared.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub enum Failure {
6464
Disconnected,
6565
}
6666

67-
impl<T: Send + 'static> Packet<T> {
67+
impl<T: Send> Packet<T> {
6868
// Creation of a packet *must* be followed by a call to postinit_lock
6969
// and later by inherit_blocker
7070
pub fn new() -> Packet<T> {
@@ -474,7 +474,7 @@ impl<T: Send + 'static> Packet<T> {
474474
}
475475

476476
#[unsafe_destructor]
477-
impl<T: Send + 'static> Drop for Packet<T> {
477+
impl<T: Send> Drop for Packet<T> {
478478
fn drop(&mut self) {
479479
// Note that this load is not only an assert for correctness about
480480
// disconnection, but also a proper fence before the read of

src/libstd/sync/mpsc/spsc_queue.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ pub struct Queue<T> {
7474
cache_subtractions: AtomicUsize,
7575
}
7676

77-
unsafe impl<T: Send + 'static> Send for Queue<T> { }
77+
unsafe impl<T: Send> Send for Queue<T> { }
7878

79-
unsafe impl<T: Send + 'static> Sync for Queue<T> { }
79+
unsafe impl<T: Send> Sync for Queue<T> { }
8080

81-
impl<T: Send + 'static> Node<T> {
81+
impl<T: Send> Node<T> {
8282
fn new() -> *mut Node<T> {
8383
unsafe {
8484
mem::transmute(box Node {
@@ -89,7 +89,7 @@ impl<T: Send + 'static> Node<T> {
8989
}
9090
}
9191

92-
impl<T: Send + 'static> Queue<T> {
92+
impl<T: Send> Queue<T> {
9393
/// Creates a new queue.
9494
///
9595
/// This is unsafe as the type system doesn't enforce a single
@@ -227,7 +227,7 @@ impl<T: Send + 'static> Queue<T> {
227227
}
228228

229229
#[unsafe_destructor]
230-
impl<T: Send + 'static> Drop for Queue<T> {
230+
impl<T: Send> Drop for Queue<T> {
231231
fn drop(&mut self) {
232232
unsafe {
233233
let mut cur = *self.first.get();

src/libstd/sync/mpsc/stream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ enum Message<T> {
7474
GoUp(Receiver<T>),
7575
}
7676

77-
impl<T: Send + 'static> Packet<T> {
77+
impl<T: Send> Packet<T> {
7878
pub fn new() -> Packet<T> {
7979
Packet {
8080
queue: unsafe { spsc::Queue::new(128) },
@@ -472,7 +472,7 @@ impl<T: Send + 'static> Packet<T> {
472472
}
473473

474474
#[unsafe_destructor]
475-
impl<T: Send + 'static> Drop for Packet<T> {
475+
impl<T: Send> Drop for Packet<T> {
476476
fn drop(&mut self) {
477477
// Note that this load is not only an assert for correctness about
478478
// disconnection, but also a proper fence before the read of

src/libstd/sync/mpsc/sync.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ pub struct Packet<T> {
5555
lock: Mutex<State<T>>,
5656
}
5757

58-
unsafe impl<T: Send + 'static> Send for Packet<T> { }
58+
unsafe impl<T: Send> Send for Packet<T> { }
5959

60-
unsafe impl<T: Send + 'static> Sync for Packet<T> { }
60+
unsafe impl<T: Send> Sync for Packet<T> { }
6161

6262
struct State<T> {
6363
disconnected: bool, // Is the channel disconnected yet?
@@ -75,7 +75,7 @@ struct State<T> {
7575
canceled: Option<&'static mut bool>,
7676
}
7777

78-
unsafe impl<T: Send + 'static> Send for State<T> {}
78+
unsafe impl<T: Send> Send for State<T> {}
7979

8080
/// Possible flavors of threads who can be blocked on this channel.
8181
enum Blocker {
@@ -113,7 +113,7 @@ pub enum Failure {
113113

114114
/// Atomically blocks the current thread, placing it into `slot`, unlocking `lock`
115115
/// in the meantime. This re-locks the mutex upon returning.
116-
fn wait<'a, 'b, T: Send + 'static>(lock: &'a Mutex<State<T>>,
116+
fn wait<'a, 'b, T: Send>(lock: &'a Mutex<State<T>>,
117117
mut guard: MutexGuard<'b, State<T>>,
118118
f: fn(SignalToken) -> Blocker)
119119
-> MutexGuard<'a, State<T>>
@@ -136,7 +136,7 @@ fn wakeup<T>(token: SignalToken, guard: MutexGuard<State<T>>) {
136136
token.signal();
137137
}
138138

139-
impl<T: Send + 'static> Packet<T> {
139+
impl<T: Send> Packet<T> {
140140
pub fn new(cap: uint) -> Packet<T> {
141141
Packet {
142142
channels: AtomicUsize::new(1),
@@ -412,7 +412,7 @@ impl<T: Send + 'static> Packet<T> {
412412
}
413413

414414
#[unsafe_destructor]
415-
impl<T: Send + 'static> Drop for Packet<T> {
415+
impl<T: Send> Drop for Packet<T> {
416416
fn drop(&mut self) {
417417
assert_eq!(self.channels.load(Ordering::SeqCst), 0);
418418
let mut guard = self.lock.lock().unwrap();

src/libstd/sync/mutex.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ pub struct Mutex<T> {
120120
data: UnsafeCell<T>,
121121
}
122122

123-
unsafe impl<T: Send + 'static> Send for Mutex<T> { }
123+
unsafe impl<T: Send> Send for Mutex<T> { }
124124

125-
unsafe impl<T: Send + 'static> Sync for Mutex<T> { }
125+
unsafe impl<T: Send> Sync for Mutex<T> { }
126126

127127
/// The static mutex type is provided to allow for static allocation of mutexes.
128128
///
@@ -180,7 +180,7 @@ pub const MUTEX_INIT: StaticMutex = StaticMutex {
180180
poison: poison::FLAG_INIT,
181181
};
182182

183-
impl<T: Send + 'static> Mutex<T> {
183+
impl<T: Send> Mutex<T> {
184184
/// Creates a new mutex in an unlocked state ready for use.
185185
#[stable(feature = "rust1", since = "1.0.0")]
186186
pub fn new(t: T) -> Mutex<T> {
@@ -243,7 +243,7 @@ impl<T: Send + 'static> Mutex<T> {
243243

244244
#[unsafe_destructor]
245245
#[stable(feature = "rust1", since = "1.0.0")]
246-
impl<T: Send + 'static> Drop for Mutex<T> {
246+
impl<T: Send> Drop for Mutex<T> {
247247
fn drop(&mut self) {
248248
// This is actually safe b/c we know that there is no further usage of
249249
// this mutex (it's up to the user to arrange for a mutex to get
@@ -354,7 +354,7 @@ mod test {
354354

355355
struct Packet<T>(Arc<(Mutex<T>, Condvar)>);
356356

357-
unsafe impl<T:'static+Send> Send for Packet<T> {}
357+
unsafe impl<T: Send> Send for Packet<T> {}
358358
unsafe impl<T> Sync for Packet<T> {}
359359

360360
#[test]

src/libstd/sync/rwlock.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ pub struct RwLock<T> {
6464
data: UnsafeCell<T>,
6565
}
6666

67-
unsafe impl<T:'static+Send> Send for RwLock<T> {}
68-
unsafe impl<T> Sync for RwLock<T> {}
67+
unsafe impl<T: Send + Sync> Send for RwLock<T> {}
68+
unsafe impl<T: Send + Sync> Sync for RwLock<T> {}
6969

7070
/// Structure representing a statically allocated RwLock.
7171
///

0 commit comments

Comments
 (0)