Skip to content

Commit 34ebc81

Browse files
committed
refactor(socket): implement ObjectInterface without intermediate methods
1 parent 2cd65dc commit 34ebc81

File tree

3 files changed

+9
-157
lines changed

3 files changed

+9
-157
lines changed

src/fd/socket/tcp.rs

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ impl Socket {
110110
})
111111
.await
112112
}
113+
}
113114

115+
#[async_trait]
116+
impl ObjectInterface for Socket {
114117
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> {
115118
future::poll_fn(|cx| {
116119
self.with(|socket| match socket.state() {
@@ -475,64 +478,3 @@ impl Drop for Socket {
475478
}
476479
}
477480
}
478-
479-
#[async_trait]
480-
impl ObjectInterface for Socket {
481-
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> {
482-
self.poll(event).await
483-
}
484-
485-
async fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
486-
self.read(buffer).await
487-
}
488-
489-
async fn write(&self, buffer: &[u8]) -> io::Result<usize> {
490-
self.write(buffer).await
491-
}
492-
493-
async fn bind(&mut self, endpoint: ListenEndpoint) -> io::Result<()> {
494-
self.bind(endpoint).await
495-
}
496-
497-
async fn connect(&mut self, endpoint: Endpoint) -> io::Result<()> {
498-
self.connect(endpoint).await
499-
}
500-
501-
async fn accept(
502-
&mut self,
503-
) -> io::Result<(Arc<async_lock::RwLock<dyn ObjectInterface>>, Endpoint)> {
504-
self.accept().await
505-
}
506-
507-
async fn getpeername(&self) -> io::Result<Option<Endpoint>> {
508-
self.getpeername().await
509-
}
510-
511-
async fn getsockname(&self) -> io::Result<Option<Endpoint>> {
512-
self.getsockname().await
513-
}
514-
515-
async fn listen(&mut self, backlog: i32) -> io::Result<()> {
516-
self.listen(backlog).await
517-
}
518-
519-
async fn setsockopt(&self, opt: SocketOption, optval: bool) -> io::Result<()> {
520-
self.setsockopt(opt, optval).await
521-
}
522-
523-
async fn getsockopt(&self, opt: SocketOption) -> io::Result<bool> {
524-
self.getsockopt(opt).await
525-
}
526-
527-
async fn shutdown(&self, how: i32) -> io::Result<()> {
528-
self.shutdown(how).await
529-
}
530-
531-
async fn status_flags(&self) -> io::Result<fd::StatusFlags> {
532-
self.status_flags().await
533-
}
534-
535-
async fn set_status_flags(&mut self, status_flags: fd::StatusFlags) -> io::Result<()> {
536-
self.set_status_flags(status_flags).await
537-
}
538-
}

src/fd/socket/udp.rs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ impl Socket {
7474
})
7575
.await
7676
}
77+
}
7778

79+
#[async_trait]
80+
impl ObjectInterface for Socket {
7881
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> {
7982
future::poll_fn(|cx| {
8083
self.with(|socket| {
@@ -251,46 +254,3 @@ impl Drop for Socket {
251254
NIC.lock().as_nic_mut().unwrap().destroy_socket(self.handle);
252255
}
253256
}
254-
255-
#[async_trait]
256-
impl ObjectInterface for Socket {
257-
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> {
258-
self.poll(event).await
259-
}
260-
261-
async fn bind(&mut self, endpoint: ListenEndpoint) -> io::Result<()> {
262-
self.bind(endpoint).await
263-
}
264-
265-
async fn connect(&mut self, endpoint: Endpoint) -> io::Result<()> {
266-
self.connect(endpoint).await
267-
}
268-
269-
async fn sendto(&self, buffer: &[u8], endpoint: Endpoint) -> io::Result<usize> {
270-
self.sendto(buffer, endpoint).await
271-
}
272-
273-
async fn recvfrom(&self, buffer: &mut [MaybeUninit<u8>]) -> io::Result<(usize, Endpoint)> {
274-
self.recvfrom(buffer).await
275-
}
276-
277-
async fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
278-
self.read(buffer).await
279-
}
280-
281-
async fn write(&self, buf: &[u8]) -> io::Result<usize> {
282-
self.write(buf).await
283-
}
284-
285-
async fn getsockname(&self) -> io::Result<Option<Endpoint>> {
286-
self.getsockname().await
287-
}
288-
289-
async fn status_flags(&self) -> io::Result<fd::StatusFlags> {
290-
self.status_flags().await
291-
}
292-
293-
async fn set_status_flags(&mut self, status_flags: fd::StatusFlags) -> io::Result<()> {
294-
self.set_status_flags(status_flags).await
295-
}
296-
}

src/fd/socket/vsock.rs

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ impl Socket {
6868
is_nonblocking: false,
6969
}
7070
}
71+
}
7172

73+
#[async_trait]
74+
impl ObjectInterface for Socket {
7275
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> {
7376
future::poll_fn(|cx| {
7477
let mut guard = VSOCK_MAP.lock();
@@ -424,56 +427,3 @@ impl Drop for Socket {
424427
guard.remove_socket(self.port);
425428
}
426429
}
427-
428-
#[async_trait]
429-
impl ObjectInterface for Socket {
430-
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> {
431-
self.poll(event).await
432-
}
433-
434-
async fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
435-
self.read(buffer).await
436-
}
437-
438-
async fn write(&self, buffer: &[u8]) -> io::Result<usize> {
439-
self.write(buffer).await
440-
}
441-
442-
async fn bind(&mut self, endpoint: ListenEndpoint) -> io::Result<()> {
443-
self.bind(endpoint).await
444-
}
445-
446-
async fn connect(&mut self, endpoint: Endpoint) -> io::Result<()> {
447-
self.connect(endpoint).await
448-
}
449-
450-
async fn accept(
451-
&mut self,
452-
) -> io::Result<(Arc<async_lock::RwLock<dyn ObjectInterface>>, Endpoint)> {
453-
self.accept().await
454-
}
455-
456-
async fn getpeername(&self) -> io::Result<Option<Endpoint>> {
457-
self.getpeername().await
458-
}
459-
460-
async fn getsockname(&self) -> io::Result<Option<Endpoint>> {
461-
self.getsockname().await
462-
}
463-
464-
async fn listen(&mut self, backlog: i32) -> io::Result<()> {
465-
self.listen(backlog).await
466-
}
467-
468-
async fn shutdown(&self, how: i32) -> io::Result<()> {
469-
self.shutdown(how).await
470-
}
471-
472-
async fn status_flags(&self) -> io::Result<fd::StatusFlags> {
473-
self.status_flags().await
474-
}
475-
476-
async fn set_status_flags(&mut self, status_flags: fd::StatusFlags) -> io::Result<()> {
477-
self.set_status_flags(status_flags).await
478-
}
479-
}

0 commit comments

Comments
 (0)