| 1 | // Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | |
| 3 | use std::ops::{Deref, DerefMut}; |
| 4 | |
| 5 | use glib::translate::TryFromGlib; |
| 6 | |
| 7 | use super::{ |
| 8 | FormattedValue, FormattedValueError, FormattedValueFullRange, FormattedValueIntrinsic, |
| 9 | GenericFormattedValue, Signed, |
| 10 | }; |
| 11 | use crate::Format; |
| 12 | |
| 13 | #[derive (PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)] |
| 14 | pub struct Undefined(i64); |
| 15 | |
| 16 | impl Undefined { |
| 17 | pub const ONE: Undefined = Undefined(1); |
| 18 | } |
| 19 | |
| 20 | // FIXME `functions in traits cannot be const` (rustc 1.64.0) |
| 21 | // rustdoc-stripper-ignore-next |
| 22 | /// `Undefined` formatted value constructor trait. |
| 23 | pub trait UndefinedFormatConstructor { |
| 24 | // rustdoc-stripper-ignore-next |
| 25 | /// Builds an `Undefined` formatted value from `self`. |
| 26 | fn undefined_format(self) -> Undefined; |
| 27 | } |
| 28 | |
| 29 | impl UndefinedFormatConstructor for i64 { |
| 30 | #[track_caller ] |
| 31 | #[inline ] |
| 32 | fn undefined_format(self) -> Undefined { |
| 33 | Undefined(self) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl FormattedValue for Undefined { |
| 38 | type FullRange = Undefined; |
| 39 | |
| 40 | #[inline ] |
| 41 | fn default_format() -> Format { |
| 42 | Format::Undefined |
| 43 | } |
| 44 | |
| 45 | #[inline ] |
| 46 | fn format(&self) -> Format { |
| 47 | Format::Undefined |
| 48 | } |
| 49 | |
| 50 | #[inline ] |
| 51 | fn is_some(&self) -> bool { |
| 52 | true |
| 53 | } |
| 54 | |
| 55 | #[inline ] |
| 56 | unsafe fn into_raw_value(self) -> i64 { |
| 57 | self.0 |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl FormattedValueFullRange for Undefined { |
| 62 | #[inline ] |
| 63 | unsafe fn from_raw(format: Format, value: i64) -> Self { |
| 64 | debug_assert_eq!(format, Format::Undefined); |
| 65 | Undefined(value) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl From<Undefined> for GenericFormattedValue { |
| 70 | #[inline ] |
| 71 | fn from(v: Undefined) -> Self { |
| 72 | skip_assert_initialized!(); |
| 73 | GenericFormattedValue::Undefined(v) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl TryFrom<GenericFormattedValue> for Undefined { |
| 78 | type Error = FormattedValueError; |
| 79 | |
| 80 | #[inline ] |
| 81 | fn try_from(v: GenericFormattedValue) -> Result<Undefined, Self::Error> { |
| 82 | skip_assert_initialized!(); |
| 83 | if let GenericFormattedValue::Undefined(v: Undefined) = v { |
| 84 | Ok(v) |
| 85 | } else { |
| 86 | Err(FormattedValueError(v.format())) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl FormattedValueIntrinsic for Undefined {} |
| 92 | |
| 93 | impl TryFromGlib<i64> for Undefined { |
| 94 | type Error = std::convert::Infallible; |
| 95 | #[inline ] |
| 96 | unsafe fn try_from_glib(v: i64) -> Result<Self, Self::Error> { |
| 97 | skip_assert_initialized!(); |
| 98 | Ok(Undefined(v)) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl From<i64> for Undefined { |
| 103 | #[inline ] |
| 104 | fn from(v: i64) -> Self { |
| 105 | skip_assert_initialized!(); |
| 106 | Undefined(v) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl Deref for Undefined { |
| 111 | type Target = i64; |
| 112 | |
| 113 | #[inline ] |
| 114 | fn deref(&self) -> &i64 { |
| 115 | &self.0 |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl DerefMut for Undefined { |
| 120 | #[inline ] |
| 121 | fn deref_mut(&mut self) -> &mut i64 { |
| 122 | &mut self.0 |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | impl AsRef<i64> for Undefined { |
| 127 | #[inline ] |
| 128 | fn as_ref(&self) -> &i64 { |
| 129 | &self.0 |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | impl AsMut<i64> for Undefined { |
| 134 | #[inline ] |
| 135 | fn as_mut(&mut self) -> &mut i64 { |
| 136 | &mut self.0 |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | impl From<Undefined> for Signed<u64> { |
| 141 | #[inline ] |
| 142 | fn from(val: Undefined) -> Signed<u64> { |
| 143 | skip_assert_initialized!(); |
| 144 | val.0.into() |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | glib_newtype_display!(Undefined, Format::Undefined); |
| 149 | |