Skip to content

Commit 358be28

Browse files
authored
refactor(quic): Endpoint (#663)
1 parent b2ddaea commit 358be28

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

compio-quic/src/endpoint.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::{
22
collections::VecDeque,
33
fmt::Debug,
4+
future::poll_fn,
45
io,
56
mem::ManuallyDrop,
67
net::{SocketAddr, SocketAddrV6},
78
ops::Deref,
89
pin::pin,
10+
ptr,
911
sync::Arc,
1012
task::{Context, Poll, Waker},
1113
time::Instant,
@@ -318,16 +320,10 @@ impl EndpointInner {
318320
pub(crate) struct EndpointRef(Shared<EndpointInner>);
319321

320322
impl EndpointRef {
321-
// Modified from [`SharedFd::try_unwrap_inner`], see notes there.
322-
unsafe fn try_unwrap_inner(&self) -> Option<EndpointInner> {
323-
let ptr = unsafe { std::ptr::read(&self.0) };
324-
match Shared::try_unwrap(ptr) {
325-
Ok(inner) => Some(inner),
326-
Err(ptr) => {
327-
std::mem::forget(ptr);
328-
None
329-
}
330-
}
323+
fn into_inner(self) -> Shared<EndpointInner> {
324+
let this = ManuallyDrop::new(self);
325+
// SAFETY: `this` is not dropped here, and we're consuming Self
326+
unsafe { ptr::read(&this.0) }
331327
}
332328

333329
async fn shutdown(self) -> io::Result<()> {
@@ -347,18 +343,21 @@ impl EndpointRef {
347343
}
348344
}
349345

350-
let this = ManuallyDrop::new(self);
351-
let inner = future::poll_fn(move |cx| {
352-
if let Some(inner) = unsafe { Self::try_unwrap_inner(&this) } {
353-
return Poll::Ready(inner);
354-
}
346+
let mut this = Some(self.into_inner());
347+
let inner = poll_fn(move |cx| {
348+
let s = match Shared::try_unwrap(this.take().unwrap()) {
349+
Ok(inner) => return Poll::Ready(inner),
350+
Err(s) => s,
351+
};
355352

356-
this.done.register(cx.waker());
353+
s.done.register(cx.waker());
357354

358-
if let Some(inner) = unsafe { Self::try_unwrap_inner(&this) } {
359-
Poll::Ready(inner)
360-
} else {
361-
Poll::Pending
355+
match Shared::try_unwrap(s) {
356+
Ok(inner) => Poll::Ready(inner),
357+
Err(s) => {
358+
this.replace(s);
359+
Poll::Pending
360+
}
362361
}
363362
})
364363
.await;

0 commit comments

Comments
 (0)