11#![ allow( dead_code) ]
22
3- use std:: {
4- mem:: { ManuallyDrop , MaybeUninit } ,
5- ptr,
6- } ;
7-
83use compio_buf:: IntoInner ;
94
105use crate :: { DriverType , OpCode } ;
@@ -52,36 +47,33 @@ impl<T: OpCode> ControlInner<T> {
5247///
5348/// [`ErasedKey`]: crate::key::ErasedKey
5449pub ( crate ) struct Carrier < T : OpCode + ?Sized > {
55- control : MaybeUninit < ControlInner < T > > ,
50+ control : ControlInner < T > ,
51+ driver_ty : DriverType ,
5652 op : T ,
5753}
5854
5955impl < T : OpCode > IntoInner for Carrier < T > {
6056 type Inner = T ;
6157
6258 fn into_inner ( self ) -> Self :: Inner {
63- let mut this = ManuallyDrop :: new ( self ) ;
64- unsafe {
65- // SAFETY: `new_uninit` ensures that the type is initialized
66- MaybeUninit :: assume_init_drop ( & mut this. control ) ;
67- // SAFETY: `self` is warpped in ManuallyDrop and is not used after here
68- ptr:: read ( & this. op )
69- }
59+ self . op
7060 }
7161}
7262
7363impl < T : OpCode > Carrier < T > {
74- /// Create a new Carrier with given OpCode.
75- ///
76- /// # Safety
77- ///
78- /// Returned [`Carrier`] must be [`initialized`] before converting to `dyn
79- /// Carry` or calling any of the `as_` getters.
80- ///
81- /// [`initialized`]: Self::init
82- pub unsafe fn new_uninit ( op : T ) -> Self {
64+ /// Create a new Carrier with given OpCode and driver type.
65+ pub fn new ( op : T , driver_ty : DriverType ) -> Self {
66+ #[ cfg( fusion) ]
67+ let control = match driver_ty {
68+ DriverType :: IoUring => ControlInner :: IoUring ( Default :: default ( ) ) ,
69+ DriverType :: Poll => ControlInner :: Poll ( Default :: default ( ) ) ,
70+ _ => unreachable ! ( "Cannot be IOCP" ) ,
71+ } ;
72+ #[ cfg( not( fusion) ) ]
73+ let control = T :: Control :: default ( ) ;
8374 Self {
84- control : MaybeUninit :: uninit ( ) ,
75+ control,
76+ driver_ty,
8577 op,
8678 }
8779 }
@@ -92,77 +84,45 @@ impl<T: OpCode> Carrier<T> {
9284 ///
9385 /// `self` must have stable address until control is no longer used, include
9486 /// all function calls via `dyn Carry` and `as_` getters.
95- pub unsafe fn init ( & mut self , driver_ty : DriverType ) {
87+ pub unsafe fn init ( & mut self ) {
9688 #[ cfg( fusion) ]
97- {
98- let control = match driver_ty {
99- DriverType :: Poll => ControlInner :: Poll ( unsafe { PollOpCode :: init ( & mut self . op ) } ) ,
100- DriverType :: IoUring => {
101- ControlInner :: IoUring ( unsafe { IourOpCode :: init ( & mut self . op ) } )
102- }
103- DriverType :: IOCP => unreachable ! ( "Cannot be windows" ) ,
89+ unsafe {
90+ match self . driver_ty {
91+ DriverType :: Poll => PollOpCode :: init ( & mut self . op , self . control . poll ( ) ) ,
92+ DriverType :: IoUring => IourOpCode :: init ( & mut self . op , self . control . iour ( ) ) ,
93+ _ => unreachable ! ( "Cannot be IOCP" ) ,
10494 } ;
105-
106- self . control . write ( control) ;
10795 }
10896
10997 #[ cfg( not( fusion) ) ]
110- {
111- _ = driver_ty;
112-
113- let control = unsafe { OpCode :: init ( & mut self . op ) } ;
114- self . control . write ( control) ;
98+ unsafe {
99+ OpCode :: init ( & mut self . op , & mut self . control )
115100 }
116101 }
117102
118103 #[ cfg( io_uring) ]
119104 pub fn as_iour ( & mut self ) -> ( & mut T , & mut <T as IourOpCode >:: Control ) {
120- // SAFETY: `new_uninit` ensures that the type is initialized
121- let control = unsafe { self . control . assume_init_mut ( ) } ;
122105 #[ cfg( fusion) ]
123- {
124- ( & mut self . op , control. iour ( ) )
125- }
106+ return ( & mut self . op , self . control . iour ( ) ) ;
126107 #[ cfg( not( fusion) ) ]
127- {
128- ( & mut self . op , control)
129- }
108+ ( & mut self . op , & mut self . control )
130109 }
131110
132111 #[ cfg( polling) ]
133112 pub fn as_poll ( & mut self ) -> ( & mut T , & mut <T as PollOpCode >:: Control ) {
134- // SAFETY: `new_uninit` ensures that the type is initialized
135- let control = unsafe { self . control . assume_init_mut ( ) } ;
136113 #[ cfg( fusion) ]
137- {
138- ( & mut self . op , control. poll ( ) )
139- }
114+ return ( & mut self . op , self . control . poll ( ) ) ;
140115 #[ cfg( not( fusion) ) ]
141- {
142- ( & mut self . op , control)
143- }
116+ ( & mut self . op , & mut self . control )
144117 }
145118
146119 #[ cfg( windows) ]
147120 pub fn as_iocp ( & self ) -> ( & T , & <T as OpCode >:: Control ) {
148- // SAFETY: `new_uninit` ensures that the type is initialized
149- let control = unsafe { self . control . assume_init_ref ( ) } ;
150-
151- ( & self . op , control)
121+ ( & self . op , & self . control )
152122 }
153123
154124 #[ cfg( windows) ]
155125 pub fn as_iocp_mut ( & mut self ) -> ( & mut T , & mut <T as OpCode >:: Control ) {
156- // SAFETY: `new_uninit` ensures that the type is initialized
157- let control = unsafe { self . control . assume_init_mut ( ) } ;
158-
159- ( & mut self . op , control)
160- }
161- }
162-
163- impl < T : OpCode + ?Sized > Drop for Carrier < T > {
164- fn drop ( & mut self ) {
165- // SAFETY: `new_uninit` ensures that the type is initialized
166- unsafe { MaybeUninit :: assume_init_drop ( & mut self . control ) } ;
126+ ( & mut self . op , & mut self . control )
167127 }
168128}
0 commit comments