vsock_service_lib/
addr.rs

1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fidl_fuchsia_hardware_vsock::Addr as Raw;
6use std::hash::{Hash, Hasher};
7use std::ops::{Deref, DerefMut};
8
9#[derive(Debug)]
10pub struct Vsock {
11    inner: Raw,
12}
13
14impl From<Raw> for Vsock {
15    fn from(addr: Raw) -> Self {
16        Vsock { inner: addr }
17    }
18}
19
20impl PartialEq for Vsock {
21    fn eq(&self, other: &Self) -> bool {
22        self.inner.local_port == other.inner.local_port
23            && self.inner.remote_port == other.inner.remote_port
24            && self.inner.remote_cid == other.inner.remote_cid
25    }
26}
27
28impl Eq for Vsock {}
29
30impl Hash for Vsock {
31    fn hash<H: Hasher>(&self, state: &mut H) {
32        self.inner.local_port.hash(state);
33        self.inner.remote_port.hash(state);
34        self.inner.remote_cid.hash(state);
35    }
36}
37
38impl Deref for Vsock {
39    type Target = Raw;
40    fn deref(&self) -> &Self::Target {
41        &self.inner
42    }
43}
44
45impl DerefMut for Vsock {
46    fn deref_mut(&mut self) -> &mut Self::Target {
47        &mut self.inner
48    }
49}
50
51impl Clone for Vsock {
52    fn clone(&self) -> Self {
53        Vsock {
54            inner: Raw {
55                local_port: self.inner.local_port,
56                remote_port: self.inner.remote_port,
57                remote_cid: self.inner.remote_cid,
58            },
59        }
60    }
61}
62
63impl Vsock {
64    pub fn new(local_port: u32, remote_port: u32, remote_cid: u32) -> Vsock {
65        Vsock { inner: Raw { local_port, remote_port, remote_cid } }
66    }
67}