gtk4/
constant_expression.rs
1use glib::{translate::*, value::FromValue};
4
5use crate::{ffi, prelude::*, ConstantExpression};
6
7define_expression!(ConstantExpression, ffi::GtkConstantExpression);
8
9impl ConstantExpression {
10 #[doc(alias = "gtk_constant_expression_new")]
19 #[doc(alias = "gtk_constant_expression_new_for_value")]
20 pub fn new(value: impl Into<glib::Value>) -> Self {
21 assert_initialized_main_thread!();
22 unsafe {
23 from_glib_full(ffi::gtk_constant_expression_new_for_value(
24 value.into().to_glib_none().0,
25 ))
26 }
27 }
28
29 #[doc(alias = "gtk_constant_expression_get_value")]
33 #[doc(alias = "get_value")]
34 pub fn value_as<V: for<'b> FromValue<'b> + 'static>(&self) -> V {
35 let value = self.value();
36 value
37 .get_owned::<V>()
38 .expect("Failed to get ConstantExpression value")
39 }
40}
41
42impl std::fmt::Debug for ConstantExpression {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 f.debug_struct("ConstantExpression")
45 .field("value_type", &self.value_type())
46 .field("is_static", &self.is_static())
47 .field("value", &self.value())
48 .finish()
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55 use crate as gtk4;
56
57 #[test]
58 fn test_expressions() {
59 let expr1 = ConstantExpression::new(23);
60 assert_eq!(expr1.value().get::<i32>().unwrap(), 23);
61 let expr2 = ConstantExpression::for_value(&"hello".to_value());
62 assert_eq!(expr2.value().get::<String>().unwrap(), "hello");
63 let expr1 = ConstantExpression::new(23);
64 assert_eq!(expr1.value().get::<i32>().unwrap(), 23);
65 assert_eq!(expr1.value_as::<i32>(), 23);
66 let expr2 = ConstantExpression::for_value(&"hello".to_value());
67 assert_eq!(expr2.value().get::<String>().unwrap(), "hello");
68 assert_eq!(expr2.value_as::<String>(), "hello");
69
70 assert!(expr1.is::<ConstantExpression>());
71 }
72}