Skip to main content

ferroui_base/input/
data_format_of_t.rs

1use super::{DataFormat, DataFormatKind};
2use std::fmt;
3use std::hash::{Hash, Hasher};
4use std::marker::PhantomData;
5use std::ops::Deref;
6
7/// Represents a format usable with the clipboard and drag-and-drop, with a
8/// data type.
9///
10/// `T` is the type of the values of the format as they are handed out:
11/// [`String`] for text, `Rc<[u8]>` for bytes, `Rc<Bitmap>` for bitmaps. It
12/// is only used to resolve the typed accessors.
13///
14/// This type cannot be instantiated directly. Use universal formats such as
15/// [`DataFormat::text`], or create custom formats using
16/// [`DataFormat::create_bytes_application_format`],
17/// [`DataFormat::create_string_application_format`],
18/// [`DataFormat::create_bytes_platform_format`],
19/// [`DataFormat::create_string_platform_format`] or
20/// [`DataFormat::create_in_process_format`].
21pub struct DataFormatOf<T> {
22    format: DataFormat,
23    _data_type: PhantomData<fn() -> T>,
24}
25
26impl<T: 'static> DataFormatOf<T> {
27    pub(super) fn new(kind: DataFormatKind, identifier: &str) -> Self {
28        Self {
29            format: DataFormat::new(kind, identifier, Some(std::any::TypeId::of::<T>())),
30            _data_type: PhantomData,
31        }
32    }
33}
34
35impl<T> DataFormatOf<T> {
36
37    /// The format without its data type.
38    #[inline]
39    pub fn as_data_format(&self) -> &DataFormat {
40        &self.format
41    }
42}
43
44impl<T> Deref for DataFormatOf<T> {
45    type Target = DataFormat;
46
47    #[inline]
48    fn deref(&self) -> &DataFormat {
49        &self.format
50    }
51}
52
53impl<T> Clone for DataFormatOf<T> {
54    fn clone(&self) -> Self {
55        Self { format: self.format.clone(), _data_type: PhantomData }
56    }
57}
58
59impl<T> From<DataFormatOf<T>> for DataFormat {
60    fn from(value: DataFormatOf<T>) -> DataFormat {
61        value.format
62    }
63}
64
65impl<T, U> PartialEq<DataFormatOf<U>> for DataFormatOf<T> {
66    #[inline]
67    fn eq(&self, other: &DataFormatOf<U>) -> bool {
68        self.format == other.format
69    }
70}
71
72impl<T> Eq for DataFormatOf<T> {}
73
74impl<T> PartialEq<DataFormat> for DataFormatOf<T> {
75    #[inline]
76    fn eq(&self, other: &DataFormat) -> bool {
77        self.format == *other
78    }
79}
80
81impl<T> PartialEq<DataFormatOf<T>> for DataFormat {
82    #[inline]
83    fn eq(&self, other: &DataFormatOf<T>) -> bool {
84        *self == other.format
85    }
86}
87
88impl<T> Hash for DataFormatOf<T> {
89    fn hash<H: Hasher>(&self, state: &mut H) {
90        self.format.hash(state)
91    }
92}
93
94impl<T> fmt::Display for DataFormatOf<T> {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        fmt::Display::fmt(&self.format, f)
97    }
98}
99
100impl<T> fmt::Debug for DataFormatOf<T> {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        fmt::Display::fmt(&self.format, f)
103    }
104}