serial_core/
disconnected.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Disconnected serial backend implementation.

use crate::SerialIo;
use futures::AsyncRead;
use futures::AsyncWrite;
use inspect::InspectMut;
use std::io;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;

/// A [`SerialIo`] implementation that is always disconnected.
#[derive(Debug, InspectMut)]
pub struct Disconnected;

impl SerialIo for Disconnected {
    fn is_connected(&self) -> bool {
        false
    }

    fn poll_connect(&mut self, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Pending
    }

    fn poll_disconnect(&mut self, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }
}

impl AsyncRead for Disconnected {
    fn poll_read(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        _buf: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        Poll::Ready(Ok(0))
    }
}

impl AsyncWrite for Disconnected {
    fn poll_write(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        _buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        Poll::Ready(Err(io::ErrorKind::BrokenPipe.into()))
    }

    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }

    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }
}

/// Resolver support for [`Disconnected`].
pub mod resolver {
    use super::Disconnected;
    use crate::resources::DisconnectedSerialBackendHandle;
    use crate::resources::ResolveSerialBackendParams;
    use crate::resources::ResolvedSerialBackend;
    use std::convert::Infallible;
    use vm_resource::IntoResource;
    use vm_resource::ResolveResource;
    use vm_resource::Resource;
    use vm_resource::declare_static_resolver;
    use vm_resource::kind::SerialBackendHandle;

    /// A resolver for [`DisconnectedSerialBackendHandle`].
    pub struct DisconnectedSerialBackendResolver;

    declare_static_resolver! {
        DisconnectedSerialBackendResolver,
        (SerialBackendHandle, DisconnectedSerialBackendHandle),
    }

    impl ResolveResource<SerialBackendHandle, DisconnectedSerialBackendHandle>
        for DisconnectedSerialBackendResolver
    {
        type Output = ResolvedSerialBackend;
        type Error = Infallible;

        fn resolve(
            &self,
            DisconnectedSerialBackendHandle: DisconnectedSerialBackendHandle,
            _input: ResolveSerialBackendParams<'_>,
        ) -> Result<Self::Output, Self::Error> {
            Ok(Disconnected.into())
        }
    }

    impl From<Disconnected> for Resource<SerialBackendHandle> {
        fn from(Disconnected: Disconnected) -> Self {
            DisconnectedSerialBackendHandle.into_resource()
        }
    }
}