sensors_lib/
client.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
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use fidl_fuchsia_sensors::ManagerControlHandle;
use std::hash::{Hash, Hasher};
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;

static CLIENT_ID_COUNT: AtomicUsize = AtomicUsize::new(0);

// TODO(375043170): Remove this when sensors have a better IPC mechanism.
//
// There is no way to compare control handles, so instead this helper struct will assign a unique
// id to each new instance. This will only be used until sensors no longer need to use
// send_on_sensor_event.
#[derive(Debug, Clone)]
pub struct Client {
    id: usize,
    pub(crate) control_handle: ManagerControlHandle,
}

impl Client {
    pub fn new(control_handle: ManagerControlHandle) -> Self {
        Self { id: CLIENT_ID_COUNT.fetch_add(1, SeqCst), control_handle }
    }
}

impl PartialEq for Client {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Client {}

impl Hash for Client {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fidl::endpoints::*;
    use fidl_fuchsia_sensors::*;

    #[fuchsia::test]
    async fn test_unique_client_ids() {
        let (_, stream) = create_proxy_and_stream::<ManagerMarker>();
        let client1 = Client::new(stream.control_handle().clone());
        let client2 = Client::new(stream.control_handle().clone());
        assert_ne!(client1.id, client2.id);
        // IDs should start at 0 and monotonically increase.
        assert_eq!(client1.id, 0);
        assert_eq!(client2.id, 1);
    }

    #[fuchsia::test]
    async fn test_client_partial_eq() {
        let (_, stream) = create_proxy_and_stream::<ManagerMarker>();
        let client1 = Client::new(stream.control_handle().clone());
        let client2 = Client::new(stream.control_handle().clone());
        assert_ne!(client1, client2);
        assert_eq!(client1, client1.clone());
    }
}