Skip to content

Commit e136f93

Browse files
committed
std: update select internals to not use mutable transmuting
1 parent d131f33 commit e136f93

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/libstd/sync/mpsc/select.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959
use core::prelude::*;
6060

61-
use core::cell::Cell;
61+
use core::cell::{Cell, UnsafeCell};
6262
use core::marker;
6363
use core::mem;
6464
use core::ptr;
@@ -70,9 +70,13 @@ use sync::mpsc::blocking::{self, SignalToken};
7070
/// The "receiver set" of the select interface. This structure is used to manage
7171
/// a set of receivers which are being selected over.
7272
pub struct Select {
73+
inner: UnsafeCell<SelectInner>,
74+
next_id: Cell<usize>,
75+
}
76+
77+
struct SelectInner {
7378
head: *mut Handle<'static, ()>,
7479
tail: *mut Handle<'static, ()>,
75-
next_id: Cell<usize>,
7680
}
7781

7882
impl !marker::Send for Select {}
@@ -84,7 +88,7 @@ pub struct Handle<'rx, T:Send+'rx> {
8488
/// The ID of this handle, used to compare against the return value of
8589
/// `Select::wait()`
8690
id: usize,
87-
selector: &'rx Select,
91+
selector: *mut SelectInner,
8892
next: *mut Handle<'static, ()>,
8993
prev: *mut Handle<'static, ()>,
9094
added: bool,
@@ -127,8 +131,10 @@ impl Select {
127131
/// ```
128132
pub fn new() -> Select {
129133
Select {
130-
head: ptr::null_mut(),
131-
tail: ptr::null_mut(),
134+
inner: UnsafeCell::new(SelectInner {
135+
head: ptr::null_mut(),
136+
tail: ptr::null_mut(),
137+
}),
132138
next_id: Cell::new(1),
133139
}
134140
}
@@ -141,7 +147,7 @@ impl Select {
141147
self.next_id.set(id + 1);
142148
Handle {
143149
id: id,
144-
selector: self,
150+
selector: self.inner.get(),
145151
next: ptr::null_mut(),
146152
prev: ptr::null_mut(),
147153
added: false,
@@ -250,7 +256,7 @@ impl Select {
250256
}
251257
}
252258

253-
fn iter(&self) -> Packets { Packets { cur: self.head } }
259+
fn iter(&self) -> Packets { Packets { cur: unsafe { &*self.inner.get() }.head } }
254260
}
255261

256262
impl<'rx, T: Send> Handle<'rx, T> {
@@ -271,7 +277,7 @@ impl<'rx, T: Send> Handle<'rx, T> {
271277
/// while it is added to the `Select` set.
272278
pub unsafe fn add(&mut self) {
273279
if self.added { return }
274-
let selector: &mut Select = mem::transmute(&*self.selector);
280+
let selector = &mut *self.selector;
275281
let me: *mut Handle<'static, ()> = mem::transmute(&*self);
276282

277283
if selector.head.is_null() {
@@ -292,7 +298,7 @@ impl<'rx, T: Send> Handle<'rx, T> {
292298
pub unsafe fn remove(&mut self) {
293299
if !self.added { return }
294300

295-
let selector: &mut Select = mem::transmute(&*self.selector);
301+
let selector = &mut *self.selector;
296302
let me: *mut Handle<'static, ()> = mem::transmute(&*self);
297303

298304
if self.prev.is_null() {
@@ -317,8 +323,10 @@ impl<'rx, T: Send> Handle<'rx, T> {
317323

318324
impl Drop for Select {
319325
fn drop(&mut self) {
320-
assert!(self.head.is_null());
321-
assert!(self.tail.is_null());
326+
unsafe {
327+
assert!((&*self.inner.get()).head.is_null());
328+
assert!((&*self.inner.get()).tail.is_null());
329+
}
322330
}
323331
}
324332

0 commit comments

Comments
 (0)