5858
5959use core:: prelude:: * ;
6060
61- use core:: cell:: Cell ;
61+ use core:: cell:: { Cell , UnsafeCell } ;
6262use core:: marker;
6363use core:: mem;
6464use 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.
7272pub 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
7882impl !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
256262impl < ' 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 ( ) {
@@ -318,8 +324,10 @@ impl<'rx, T: Send> Handle<'rx, T> {
318324#[ unsafe_destructor]
319325impl Drop for Select {
320326 fn drop ( & mut self ) {
321- assert ! ( self . head. is_null( ) ) ;
322- assert ! ( self . tail. is_null( ) ) ;
327+ unsafe {
328+ assert ! ( ( & * self . inner. get( ) ) . head. is_null( ) ) ;
329+ assert ! ( ( & * self . inner. get( ) ) . tail. is_null( ) ) ;
330+ }
323331 }
324332}
325333
0 commit comments