serial_core/
disconnected.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Disconnected serial backend implementation.
5
6use crate::SerialIo;
7use futures::AsyncRead;
8use futures::AsyncWrite;
9use inspect::InspectMut;
10use std::io;
11use std::pin::Pin;
12use std::task::Context;
13use std::task::Poll;
14
15/// A [`SerialIo`] implementation that is always disconnected.
16#[derive(Debug, InspectMut)]
17pub struct Disconnected;
18
19impl SerialIo for Disconnected {
20    fn is_connected(&self) -> bool {
21        false
22    }
23
24    fn poll_connect(&mut self, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
25        Poll::Pending
26    }
27
28    fn poll_disconnect(&mut self, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
29        Poll::Ready(Ok(()))
30    }
31}
32
33impl AsyncRead for Disconnected {
34    fn poll_read(
35        self: Pin<&mut Self>,
36        _cx: &mut Context<'_>,
37        _buf: &mut [u8],
38    ) -> Poll<io::Result<usize>> {
39        Poll::Ready(Ok(0))
40    }
41}
42
43impl AsyncWrite for Disconnected {
44    fn poll_write(
45        self: Pin<&mut Self>,
46        _cx: &mut Context<'_>,
47        _buf: &[u8],
48    ) -> Poll<io::Result<usize>> {
49        Poll::Ready(Err(io::ErrorKind::BrokenPipe.into()))
50    }
51
52    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
53        Poll::Ready(Ok(()))
54    }
55
56    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
57        Poll::Ready(Ok(()))
58    }
59}
60
61/// Resolver support for [`Disconnected`].
62pub mod resolver {
63    use super::Disconnected;
64    use crate::resources::DisconnectedSerialBackendHandle;
65    use crate::resources::ResolveSerialBackendParams;
66    use crate::resources::ResolvedSerialBackend;
67    use std::convert::Infallible;
68    use vm_resource::IntoResource;
69    use vm_resource::ResolveResource;
70    use vm_resource::Resource;
71    use vm_resource::declare_static_resolver;
72    use vm_resource::kind::SerialBackendHandle;
73
74    /// A resolver for [`DisconnectedSerialBackendHandle`].
75    pub struct DisconnectedSerialBackendResolver;
76
77    declare_static_resolver! {
78        DisconnectedSerialBackendResolver,
79        (SerialBackendHandle, DisconnectedSerialBackendHandle),
80    }
81
82    impl ResolveResource<SerialBackendHandle, DisconnectedSerialBackendHandle>
83        for DisconnectedSerialBackendResolver
84    {
85        type Output = ResolvedSerialBackend;
86        type Error = Infallible;
87
88        fn resolve(
89            &self,
90            DisconnectedSerialBackendHandle: DisconnectedSerialBackendHandle,
91            _input: ResolveSerialBackendParams<'_>,
92        ) -> Result<Self::Output, Self::Error> {
93            Ok(Disconnected.into())
94        }
95    }
96
97    impl From<Disconnected> for Resource<SerialBackendHandle> {
98        fn from(Disconnected: Disconnected) -> Self {
99            DisconnectedSerialBackendHandle.into_resource()
100        }
101    }
102}