Skip to main content

usb_gadget/function/
mod.rs

1//! USB gadget functions.
2
3pub mod audio;
4pub mod custom;
5pub mod hid;
6pub mod loopback;
7pub mod midi;
8pub mod msd;
9pub mod net;
10pub mod other;
11pub mod printer;
12pub mod serial;
13pub mod sourcesink;
14pub mod util;
15pub mod video;
16
17use std::{cmp, hash, hash::Hash, sync::Arc};
18
19use self::util::{register_remove_handler, Function};
20
21/// USB gadget function handle.
22///
23/// Use a member of the [function module](crate::function) to obtain a
24/// gadget function handle.
25#[derive(Debug, Clone)]
26pub struct Handle(Arc<dyn Function>);
27
28impl Handle {
29    pub(crate) fn new<F: Function>(f: F) -> Self {
30        Self(Arc::new(f))
31    }
32}
33
34impl Handle {
35    pub(crate) fn get(&self) -> &dyn Function {
36        &*self.0
37    }
38}
39
40impl PartialEq for Handle {
41    fn eq(&self, other: &Self) -> bool {
42        Arc::ptr_eq(&self.0, &other.0)
43    }
44}
45
46impl Eq for Handle {}
47
48impl PartialOrd for Handle {
49    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
50        Some(self.cmp(other))
51    }
52}
53
54impl Ord for Handle {
55    fn cmp(&self, other: &Self) -> cmp::Ordering {
56        Arc::as_ptr(&self.0).cast::<()>().cmp(&Arc::as_ptr(&other.0).cast::<()>())
57    }
58}
59
60impl Hash for Handle {
61    fn hash<H: hash::Hasher>(&self, state: &mut H) {
62        Arc::as_ptr(&self.0).hash(state);
63    }
64}
65
66/// Register included remove handlers.
67fn register_remove_handlers() {
68    register_remove_handler(custom::driver(), custom::remove_handler);
69    register_remove_handler(msd::driver(), msd::remove_handler);
70    register_remove_handler(video::driver(), video::remove_handler);
71}