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
use glib::{value::ToValue, Value};

pub trait ToValueOption: Sized {
	type Type: Into<Self>;

	fn to_value_option(self) -> Option<Value>;
}

pub struct PrimitiveValue<T>(T);
impl<T> From<T> for PrimitiveValue<T> {
	fn from(v: T) -> Self {
		Self(v)
	}
}
impl ToValueOption for PrimitiveValue<()> {
	type Type = ();

	fn to_value_option(self) -> Option<Value> {
		None
	}
}
impl ToValueOption for PrimitiveValue<usize> {
	type Type = usize;

	#[cfg(target_pointer_width = "16")]
	fn to_value_option(self) -> Option<Value> {
		Some((self.0 as u16).to_value())
	}

	#[cfg(target_pointer_width = "32")]
	fn to_value_option(self) -> Option<Value> {
		Some((self.0 as u32).to_value())
	}

	#[cfg(target_pointer_width = "64")]
	fn to_value_option(self) -> Option<Value> {
		Some((self.0 as u64).to_value())
	}
}

impl<T: ToValue> ToValueOption for T {
	type Type = T;

	fn to_value_option(self) -> Option<Value> {
		Some(ToValue::to_value(&self))
	}
}