1use std::{cmp, ffi::CStr, fmt, ops::Deref, ptr};
4
5use crate::{
6 ffi, gobject_ffi, prelude::*, translate::*, ParamSpecEnum, ParamSpecFlags, Type, TypeInfo,
7 Value,
8};
9
10#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
11pub enum UserDirectory {
12 #[doc(alias = "G_USER_DIRECTORY_DESKTOP")]
13 Desktop,
14 #[doc(alias = "G_USER_DIRECTORY_DOCUMENTS")]
15 Documents,
16 #[doc(alias = "G_USER_DIRECTORY_DOWNLOAD")]
17 Downloads,
18 #[doc(alias = "G_USER_DIRECTORY_MUSIC")]
19 Music,
20 #[doc(alias = "G_USER_DIRECTORY_PICTURES")]
21 Pictures,
22 #[doc(alias = "G_USER_DIRECTORY_PUBLIC_SHARE")]
23 PublicShare,
24 #[doc(alias = "G_USER_DIRECTORY_TEMPLATES")]
25 Templates,
26 #[doc(alias = "G_USER_DIRECTORY_VIDEOS")]
27 Videos,
28}
29
30#[doc(hidden)]
31impl IntoGlib for UserDirectory {
32 type GlibType = ffi::GUserDirectory;
33
34 #[inline]
35 fn into_glib(self) -> ffi::GUserDirectory {
36 match self {
37 Self::Desktop => ffi::G_USER_DIRECTORY_DESKTOP,
38 Self::Documents => ffi::G_USER_DIRECTORY_DOCUMENTS,
39 Self::Downloads => ffi::G_USER_DIRECTORY_DOWNLOAD,
40 Self::Music => ffi::G_USER_DIRECTORY_MUSIC,
41 Self::Pictures => ffi::G_USER_DIRECTORY_PICTURES,
42 Self::PublicShare => ffi::G_USER_DIRECTORY_PUBLIC_SHARE,
43 Self::Templates => ffi::G_USER_DIRECTORY_TEMPLATES,
44 Self::Videos => ffi::G_USER_DIRECTORY_VIDEOS,
45 }
46 }
47}
48
49#[doc(alias = "GEnumClass")]
53#[repr(transparent)]
54pub struct EnumClass(ptr::NonNull<gobject_ffi::GEnumClass>);
55
56unsafe impl Send for EnumClass {}
57unsafe impl Sync for EnumClass {}
58
59impl fmt::Debug for EnumClass {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 f.debug_struct("EnumClass")
62 .field("type", &self.type_())
63 .field("values", &self.values())
64 .finish()
65 }
66}
67
68impl EnumClass {
69 pub fn new<T: StaticType + HasParamSpec<ParamSpec = ParamSpecEnum>>() -> Self {
74 Self::with_type(T::static_type()).expect("invalid enum class")
75 }
76 pub fn with_type(type_: Type) -> Option<Self> {
81 unsafe {
82 let is_enum: bool = from_glib(gobject_ffi::g_type_is_a(
83 type_.into_glib(),
84 gobject_ffi::G_TYPE_ENUM,
85 ));
86 if !is_enum {
87 return None;
88 }
89
90 Some(EnumClass(
91 ptr::NonNull::new(gobject_ffi::g_type_class_ref(type_.into_glib()) as *mut _)
92 .unwrap(),
93 ))
94 }
95 }
96
97 pub fn type_(&self) -> Type {
100 unsafe { from_glib(self.0.as_ref().g_type_class.g_type) }
101 }
102
103 #[doc(alias = "g_enum_get_value")]
109 #[doc(alias = "get_value")]
110 pub fn value(&self, value: i32) -> Option<&EnumValue> {
111 unsafe {
112 let v = gobject_ffi::g_enum_get_value(self.0.as_ptr(), value);
113 if v.is_null() {
114 None
115 } else {
116 Some(&*(v as *const EnumValue))
117 }
118 }
119 }
120
121 #[doc(alias = "g_enum_get_value_by_name")]
127 #[doc(alias = "get_value_by_name")]
128 pub fn value_by_name(&self, name: &str) -> Option<&EnumValue> {
129 unsafe {
130 let v = gobject_ffi::g_enum_get_value_by_name(self.0.as_ptr(), name.to_glib_none().0);
131 if v.is_null() {
132 None
133 } else {
134 Some(&*(v as *const EnumValue))
135 }
136 }
137 }
138
139 #[doc(alias = "g_enum_get_value_by_nick")]
145 #[doc(alias = "get_value_by_nick")]
146 pub fn value_by_nick(&self, nick: &str) -> Option<&EnumValue> {
147 unsafe {
148 let v = gobject_ffi::g_enum_get_value_by_nick(self.0.as_ptr(), nick.to_glib_none().0);
149 if v.is_null() {
150 None
151 } else {
152 Some(&*(v as *const EnumValue))
153 }
154 }
155 }
156
157 #[doc(alias = "get_values")]
160 pub fn values(&self) -> &[EnumValue] {
161 unsafe {
162 if self.0.as_ref().n_values == 0 {
163 return &[];
164 }
165 std::slice::from_raw_parts(
166 self.0.as_ref().values as *const EnumValue,
167 self.0.as_ref().n_values as usize,
168 )
169 }
170 }
171
172 pub fn to_value(&self, value: i32) -> Option<Value> {
175 self.value(value).map(|v| v.to_value(self))
176 }
177
178 pub fn to_value_by_name(&self, name: &str) -> Option<Value> {
181 self.value_by_name(name).map(|v| v.to_value(self))
182 }
183
184 pub fn to_value_by_nick(&self, nick: &str) -> Option<Value> {
187 self.value_by_nick(nick).map(|v| v.to_value(self))
188 }
189
190 #[doc(alias = "g_enum_complete_type_info")]
200 pub fn complete_type_info(
201 type_: Type,
202 const_static_values: &'static EnumValues,
203 ) -> Option<TypeInfo> {
204 unsafe {
205 let is_enum: bool = from_glib(gobject_ffi::g_type_is_a(
206 type_.into_glib(),
207 gobject_ffi::G_TYPE_ENUM,
208 ));
209 if !is_enum {
210 return None;
211 }
212
213 let info = TypeInfo::default();
214 gobject_ffi::g_enum_complete_type_info(
215 type_.into_glib(),
216 info.as_ptr(),
217 const_static_values.to_glib_none().0,
218 );
219 Some(info)
220 }
221 }
222}
223
224impl Drop for EnumClass {
225 #[inline]
226 fn drop(&mut self) {
227 unsafe {
228 gobject_ffi::g_type_class_unref(self.0.as_ptr() as *mut _);
229 }
230 }
231}
232
233impl Clone for EnumClass {
234 #[inline]
235 fn clone(&self) -> Self {
236 unsafe {
237 Self(ptr::NonNull::new(gobject_ffi::g_type_class_ref(self.type_().into_glib()) as *mut _).unwrap())
238 }
239 }
240}
241
242#[doc(alias = "GEnumValue")]
245#[derive(Copy, Clone)]
246#[repr(transparent)]
247pub struct EnumValue(gobject_ffi::GEnumValue);
248
249unsafe impl Send for EnumValue {}
250unsafe impl Sync for EnumValue {}
251
252impl fmt::Debug for EnumValue {
253 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
254 f.debug_struct("EnumValue")
255 .field("value", &self.value())
256 .field("name", &self.name())
257 .field("nick", &self.nick())
258 .finish()
259 }
260}
261
262impl EnumValue {
263 pub const unsafe fn unsafe_from(g_value: gobject_ffi::GEnumValue) -> Self {
269 Self(g_value)
270 }
271
272 #[doc(alias = "get_value")]
275 pub fn value(&self) -> i32 {
276 self.0.value
277 }
278
279 #[doc(alias = "get_name")]
282 pub fn name(&self) -> &str {
283 unsafe { CStr::from_ptr(self.0.value_name).to_str().unwrap() }
284 }
285
286 #[doc(alias = "get_nick")]
289 pub fn nick(&self) -> &str {
290 unsafe { CStr::from_ptr(self.0.value_nick).to_str().unwrap() }
291 }
292
293 pub fn to_value(&self, enum_: &EnumClass) -> Value {
296 unsafe {
297 let mut v = Value::from_type_unchecked(enum_.type_());
298 gobject_ffi::g_value_set_enum(v.to_glib_none_mut().0, self.0.value);
299 v
300 }
301 }
302
303 pub fn from_value(value: &Value) -> Option<(EnumClass, &EnumValue)> {
306 unsafe {
307 let enum_class = EnumClass::with_type(value.type_())?;
308 let v = enum_class.value(gobject_ffi::g_value_get_enum(value.to_glib_none().0))?;
309 let v = &*(v as *const EnumValue);
310 Some((enum_class, v))
311 }
312 }
313}
314
315impl PartialEq for EnumValue {
316 fn eq(&self, other: &Self) -> bool {
317 self.value().eq(&other.value())
318 }
319}
320
321impl Eq for EnumValue {}
322
323impl PartialOrd for EnumValue {
324 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
325 Some(self.cmp(other))
326 }
327}
328
329impl Ord for EnumValue {
330 fn cmp(&self, other: &Self) -> cmp::Ordering {
331 self.value().cmp(&other.value())
332 }
333}
334
335impl UnsafeFrom<gobject_ffi::GEnumValue> for EnumValue {
336 unsafe fn unsafe_from(g_value: gobject_ffi::GEnumValue) -> Self {
337 Self::unsafe_from(g_value)
338 }
339}
340
341unsafe impl<'a> crate::value::FromValue<'a> for &EnumValue {
342 type Checker = EnumTypeChecker;
343
344 unsafe fn from_value(value: &'a Value) -> Self {
345 let (_, v) = EnumValue::from_value(value).unwrap();
346 std::mem::transmute(v)
348 }
349}
350
351impl EnumerationValue<EnumValue> for EnumValue {
354 type GlibType = gobject_ffi::GEnumValue;
355 const ZERO: EnumValue = unsafe {
356 EnumValue::unsafe_from(gobject_ffi::GEnumValue {
357 value: 0,
358 value_name: ptr::null(),
359 value_nick: ptr::null(),
360 })
361 };
362}
363
364pub type EnumValuesStorage<const N: usize> = EnumerationValuesStorage<EnumValue, N>;
367
368pub type EnumValues = EnumerationValues<EnumValue>;
371
372pub struct EnumTypeChecker();
373unsafe impl crate::value::ValueTypeChecker for EnumTypeChecker {
374 type Error = InvalidEnumError;
375
376 fn check(value: &Value) -> Result<(), Self::Error> {
377 let t = value.type_();
378 if t.is_a(Type::ENUM) {
379 Ok(())
380 } else {
381 Err(InvalidEnumError)
382 }
383 }
384}
385
386#[derive(Clone, PartialEq, Eq, Debug)]
390pub struct InvalidEnumError;
391
392impl fmt::Display for InvalidEnumError {
393 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
394 write!(f, "Value is not an enum")
395 }
396}
397
398impl std::error::Error for InvalidEnumError {}
399
400#[doc(alias = "GFlagsClass")]
404#[repr(transparent)]
405pub struct FlagsClass(ptr::NonNull<gobject_ffi::GFlagsClass>);
406
407unsafe impl Send for FlagsClass {}
408unsafe impl Sync for FlagsClass {}
409
410impl fmt::Debug for FlagsClass {
411 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
412 f.debug_struct("FlagsClass")
413 .field("type", &self.type_())
414 .field("values", &self.values())
415 .finish()
416 }
417}
418
419impl FlagsClass {
420 pub fn new<T: StaticType + HasParamSpec<ParamSpec = ParamSpecFlags>>() -> Self {
425 Self::with_type(T::static_type()).expect("invalid flags class")
426 }
427 pub fn with_type(type_: Type) -> Option<Self> {
432 unsafe {
433 let is_flags: bool = from_glib(gobject_ffi::g_type_is_a(
434 type_.into_glib(),
435 gobject_ffi::G_TYPE_FLAGS,
436 ));
437 if !is_flags {
438 return None;
439 }
440
441 Some(FlagsClass(
442 ptr::NonNull::new(gobject_ffi::g_type_class_ref(type_.into_glib()) as *mut _)
443 .unwrap(),
444 ))
445 }
446 }
447
448 pub fn type_(&self) -> Type {
451 unsafe { from_glib(self.0.as_ref().g_type_class.g_type) }
452 }
453
454 #[doc(alias = "g_flags_get_first_value")]
460 #[doc(alias = "get_value")]
461 pub fn value(&self, value: u32) -> Option<&FlagsValue> {
462 unsafe {
463 let v = gobject_ffi::g_flags_get_first_value(self.0.as_ptr(), value);
464 if v.is_null() {
465 None
466 } else {
467 Some(&*(v as *const FlagsValue))
468 }
469 }
470 }
471
472 #[doc(alias = "g_flags_get_value_by_name")]
478 #[doc(alias = "get_value_by_name")]
479 pub fn value_by_name(&self, name: &str) -> Option<&FlagsValue> {
480 unsafe {
481 let v = gobject_ffi::g_flags_get_value_by_name(self.0.as_ptr(), name.to_glib_none().0);
482 if v.is_null() {
483 None
484 } else {
485 Some(&*(v as *const FlagsValue))
486 }
487 }
488 }
489
490 #[doc(alias = "g_flags_get_value_by_nick")]
496 #[doc(alias = "get_value_by_nick")]
497 pub fn value_by_nick(&self, nick: &str) -> Option<&FlagsValue> {
498 unsafe {
499 let v = gobject_ffi::g_flags_get_value_by_nick(self.0.as_ptr(), nick.to_glib_none().0);
500 if v.is_null() {
501 None
502 } else {
503 Some(&*(v as *const FlagsValue))
504 }
505 }
506 }
507
508 #[doc(alias = "get_values")]
511 pub fn values(&self) -> &[FlagsValue] {
512 unsafe {
513 if self.0.as_ref().n_values == 0 {
514 return &[];
515 }
516 std::slice::from_raw_parts(
517 self.0.as_ref().values as *const FlagsValue,
518 self.0.as_ref().n_values as usize,
519 )
520 }
521 }
522
523 pub fn to_value(&self, value: u32) -> Option<Value> {
526 self.value(value).map(|v| v.to_value(self))
527 }
528
529 pub fn to_value_by_name(&self, name: &str) -> Option<Value> {
532 self.value_by_name(name).map(|v| v.to_value(self))
533 }
534
535 pub fn to_value_by_nick(&self, nick: &str) -> Option<Value> {
538 self.value_by_nick(nick).map(|v| v.to_value(self))
539 }
540
541 pub fn is_set(&self, value: &Value, f: u32) -> bool {
544 unsafe {
545 if self.type_() != value.type_() {
546 return false;
547 }
548
549 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
550 flags & f != 0
551 }
552 }
553
554 pub fn is_set_by_name(&self, value: &Value, name: &str) -> bool {
557 unsafe {
558 if self.type_() != value.type_() {
559 return false;
560 }
561
562 if let Some(f) = self.value_by_name(name) {
563 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
564 flags & f.value() != 0
565 } else {
566 false
567 }
568 }
569 }
570
571 pub fn is_set_by_nick(&self, value: &Value, nick: &str) -> bool {
574 unsafe {
575 if self.type_() != value.type_() {
576 return false;
577 }
578
579 if let Some(f) = self.value_by_nick(nick) {
580 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
581 flags & f.value() != 0
582 } else {
583 false
584 }
585 }
586 }
587
588 #[doc(alias = "g_value_set_flags")]
595 pub fn set(&self, mut value: Value, f: u32) -> Result<Value, Value> {
596 unsafe {
597 if self.type_() != value.type_() {
598 return Err(value);
599 }
600
601 if let Some(f) = self.value(f) {
602 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
603 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags | f.value());
604 Ok(value)
605 } else {
606 Err(value)
607 }
608 }
609 }
610
611 pub fn set_by_name(&self, mut value: Value, name: &str) -> Result<Value, Value> {
618 unsafe {
619 if self.type_() != value.type_() {
620 return Err(value);
621 }
622
623 if let Some(f) = self.value_by_name(name) {
624 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
625 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags | f.value());
626 Ok(value)
627 } else {
628 Err(value)
629 }
630 }
631 }
632
633 pub fn set_by_nick(&self, mut value: Value, nick: &str) -> Result<Value, Value> {
640 unsafe {
641 if self.type_() != value.type_() {
642 return Err(value);
643 }
644
645 if let Some(f) = self.value_by_nick(nick) {
646 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
647 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags | f.value());
648 Ok(value)
649 } else {
650 Err(value)
651 }
652 }
653 }
654
655 pub fn unset(&self, mut value: Value, f: u32) -> Result<Value, Value> {
662 unsafe {
663 if self.type_() != value.type_() {
664 return Err(value);
665 }
666
667 if let Some(f) = self.value(f) {
668 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
669 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags & !f.value());
670 Ok(value)
671 } else {
672 Err(value)
673 }
674 }
675 }
676
677 pub fn unset_by_name(&self, mut value: Value, name: &str) -> Result<Value, Value> {
684 unsafe {
685 if self.type_() != value.type_() {
686 return Err(value);
687 }
688
689 if let Some(f) = self.value_by_name(name) {
690 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
691 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags & !f.value());
692 Ok(value)
693 } else {
694 Err(value)
695 }
696 }
697 }
698
699 pub fn unset_by_nick(&self, mut value: Value, nick: &str) -> Result<Value, Value> {
706 unsafe {
707 if self.type_() != value.type_() {
708 return Err(value);
709 }
710
711 if let Some(f) = self.value_by_nick(nick) {
712 let flags = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
713 gobject_ffi::g_value_set_flags(value.to_glib_none_mut().0, flags & !f.value());
714 Ok(value)
715 } else {
716 Err(value)
717 }
718 }
719 }
720
721 pub fn to_nick_string(&self, mut value: u32) -> String {
724 let mut s = String::new();
725 for val in self.values() {
726 let v = val.value();
727 if v != 0 && (value & v) == v {
728 value &= !v;
729 if !s.is_empty() {
730 s.push('|');
731 }
732 s.push_str(val.nick());
733 }
734 }
735 s
736 }
737
738 pub fn from_nick_string(&self, s: &str) -> Result<u32, ParseFlagsError> {
741 s.split('|').try_fold(0u32, |acc, flag| {
742 self.value_by_nick(flag.trim())
743 .map(|v| acc + v.value())
744 .ok_or_else(|| ParseFlagsError(flag.to_owned()))
745 })
746 }
747
748 pub fn builder(&self) -> FlagsBuilder {
752 FlagsBuilder::new(self)
753 }
754
755 pub fn builder_with_value(&self, value: Value) -> Option<FlagsBuilder> {
759 if self.type_() != value.type_() {
760 return None;
761 }
762
763 Some(FlagsBuilder::with_value(self, value))
764 }
765
766 #[doc(alias = "g_flags_complete_type_info")]
776 pub fn complete_type_info(
777 type_: Type,
778 const_static_values: &'static FlagsValues,
779 ) -> Option<TypeInfo> {
780 unsafe {
781 let is_flags: bool = from_glib(gobject_ffi::g_type_is_a(
782 type_.into_glib(),
783 gobject_ffi::G_TYPE_FLAGS,
784 ));
785 if !is_flags {
786 return None;
787 }
788
789 let info = TypeInfo::default();
790 gobject_ffi::g_flags_complete_type_info(
791 type_.into_glib(),
792 info.as_ptr(),
793 const_static_values.to_glib_none().0,
794 );
795 Some(info)
796 }
797 }
798}
799
800impl Drop for FlagsClass {
801 #[inline]
802 fn drop(&mut self) {
803 unsafe {
804 gobject_ffi::g_type_class_unref(self.0.as_ptr() as *mut _);
805 }
806 }
807}
808
809impl Clone for FlagsClass {
810 #[inline]
811 fn clone(&self) -> Self {
812 unsafe {
813 Self(ptr::NonNull::new(gobject_ffi::g_type_class_ref(self.type_().into_glib()) as *mut _).unwrap())
814 }
815 }
816}
817
818#[derive(Debug)]
819pub struct ParseFlagsError(String);
820
821impl std::error::Error for ParseFlagsError {}
822
823impl fmt::Display for ParseFlagsError {
824 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
825 write!(f, "Unknown flag: '{}'", self.0)
826 }
827}
828
829impl ParseFlagsError {
830 pub fn flag(&self) -> &str {
831 &self.0
832 }
833}
834
835#[doc(alias = "GFlagsValue")]
838#[derive(Copy, Clone)]
839#[repr(transparent)]
840pub struct FlagsValue(gobject_ffi::GFlagsValue);
841
842unsafe impl Send for FlagsValue {}
843unsafe impl Sync for FlagsValue {}
844
845impl fmt::Debug for FlagsValue {
846 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
847 f.debug_struct("FlagsValue")
848 .field("value", &self.value())
849 .field("name", &self.name())
850 .field("nick", &self.nick())
851 .finish()
852 }
853}
854
855impl FlagsValue {
856 pub const unsafe fn unsafe_from(g_value: gobject_ffi::GFlagsValue) -> Self {
862 Self(g_value)
863 }
864
865 #[doc(alias = "get_value")]
868 pub fn value(&self) -> u32 {
869 self.0.value
870 }
871
872 #[doc(alias = "get_name")]
875 pub fn name(&self) -> &str {
876 unsafe { CStr::from_ptr(self.0.value_name).to_str().unwrap() }
877 }
878
879 #[doc(alias = "get_nick")]
882 pub fn nick(&self) -> &str {
883 unsafe { CStr::from_ptr(self.0.value_nick).to_str().unwrap() }
884 }
885
886 pub fn to_value(&self, flags: &FlagsClass) -> Value {
889 unsafe {
890 let mut v = Value::from_type_unchecked(flags.type_());
891 gobject_ffi::g_value_set_flags(v.to_glib_none_mut().0, self.0.value);
892 v
893 }
894 }
895
896 pub fn from_value(value: &Value) -> Option<(FlagsClass, Vec<&FlagsValue>)> {
899 unsafe {
900 let flags_class = FlagsClass::with_type(value.type_())?;
901 let mut res = Vec::new();
902 let f = gobject_ffi::g_value_get_flags(value.to_glib_none().0);
903 for v in flags_class.values() {
904 if v.value() & f != 0 {
905 res.push(&*(v as *const FlagsValue));
906 }
907 }
908 Some((flags_class, res))
909 }
910 }
911}
912
913impl PartialEq for FlagsValue {
914 fn eq(&self, other: &Self) -> bool {
915 self.value().eq(&other.value())
916 }
917}
918
919impl Eq for FlagsValue {}
920
921impl UnsafeFrom<gobject_ffi::GFlagsValue> for FlagsValue {
922 unsafe fn unsafe_from(g_value: gobject_ffi::GFlagsValue) -> Self {
923 Self::unsafe_from(g_value)
924 }
925}
926
927impl EnumerationValue<FlagsValue> for FlagsValue {
930 type GlibType = gobject_ffi::GFlagsValue;
931 const ZERO: FlagsValue = unsafe {
932 FlagsValue::unsafe_from(gobject_ffi::GFlagsValue {
933 value: 0,
934 value_name: ptr::null(),
935 value_nick: ptr::null(),
936 })
937 };
938}
939
940pub type FlagsValuesStorage<const N: usize> = EnumerationValuesStorage<FlagsValue, N>;
943
944pub type FlagsValues = EnumerationValues<FlagsValue>;
947
948#[must_use = "The builder must be built to be used"]
967pub struct FlagsBuilder<'a>(&'a FlagsClass, Option<Value>);
968impl FlagsBuilder<'_> {
969 fn new(flags_class: &FlagsClass) -> FlagsBuilder {
970 let value = unsafe { Value::from_type_unchecked(flags_class.type_()) };
971 FlagsBuilder(flags_class, Some(value))
972 }
973
974 fn with_value(flags_class: &FlagsClass, value: Value) -> FlagsBuilder {
975 FlagsBuilder(flags_class, Some(value))
976 }
977
978 pub fn set(mut self, f: u32) -> Self {
981 if let Some(value) = self.1.take() {
982 self.1 = self.0.set(value, f).ok();
983 }
984
985 self
986 }
987
988 pub fn set_by_name(mut self, name: &str) -> Self {
991 if let Some(value) = self.1.take() {
992 self.1 = self.0.set_by_name(value, name).ok();
993 }
994
995 self
996 }
997
998 pub fn set_by_nick(mut self, nick: &str) -> Self {
1001 if let Some(value) = self.1.take() {
1002 self.1 = self.0.set_by_nick(value, nick).ok();
1003 }
1004
1005 self
1006 }
1007
1008 pub fn unset(mut self, f: u32) -> Self {
1011 if let Some(value) = self.1.take() {
1012 self.1 = self.0.unset(value, f).ok();
1013 }
1014
1015 self
1016 }
1017
1018 pub fn unset_by_name(mut self, name: &str) -> Self {
1021 if let Some(value) = self.1.take() {
1022 self.1 = self.0.unset_by_name(value, name).ok();
1023 }
1024
1025 self
1026 }
1027
1028 pub fn unset_by_nick(mut self, nick: &str) -> Self {
1031 if let Some(value) = self.1.take() {
1032 self.1 = self.0.unset_by_nick(value, nick).ok();
1033 }
1034
1035 self
1036 }
1037
1038 #[must_use = "Value returned from the builder should probably be used"]
1041 pub fn build(self) -> Option<Value> {
1042 self.1
1043 }
1044}
1045
1046unsafe impl<'a> crate::value::FromValue<'a> for Vec<&FlagsValue> {
1047 type Checker = FlagsTypeChecker;
1048
1049 unsafe fn from_value(value: &'a Value) -> Self {
1050 let (_, v) = FlagsValue::from_value(value).unwrap();
1051 std::mem::transmute(v)
1053 }
1054}
1055
1056pub struct FlagsTypeChecker();
1057unsafe impl crate::value::ValueTypeChecker for FlagsTypeChecker {
1058 type Error = InvalidFlagsError;
1059
1060 fn check(value: &Value) -> Result<(), Self::Error> {
1061 let t = value.type_();
1062 if t.is_a(Type::FLAGS) {
1063 Ok(())
1064 } else {
1065 Err(InvalidFlagsError)
1066 }
1067 }
1068}
1069
1070#[derive(Clone, PartialEq, Eq, Debug)]
1074pub struct InvalidFlagsError;
1075
1076impl fmt::Display for InvalidFlagsError {
1077 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1078 write!(f, "Value is not a flags")
1079 }
1080}
1081
1082impl std::error::Error for InvalidFlagsError {}
1083
1084pub trait EnumerationValue<E>: Copy {
1087 type GlibType;
1088 const ZERO: E;
1089}
1090
1091#[repr(C)]
1102pub struct EnumerationValuesStorage<E: EnumerationValue<E>, const S: usize>([E; S]);
1103
1104impl<E: EnumerationValue<E>, const S: usize> EnumerationValuesStorage<E, S> {
1105 pub const fn new<const N: usize>(values: [E; N]) -> Self {
1108 #[repr(C)]
1109 #[derive(Copy, Clone)]
1110 struct Both<E: Copy, const N: usize>([E; N], [E; 1]);
1111
1112 #[repr(C)]
1113 union Transmute<E: Copy, const N: usize, const S: usize> {
1114 from: Both<E, N>,
1115 to: [E; S],
1116 }
1117
1118 unsafe {
1120 let all = Transmute {
1122 from: Both(values, [E::ZERO; 1]),
1123 }
1124 .to;
1125 Self(all)
1126 }
1127 }
1128}
1129
1130impl<E: EnumerationValue<E>, const S: usize> AsRef<EnumerationValues<E>>
1131 for EnumerationValuesStorage<E, S>
1132{
1133 fn as_ref(&self) -> &EnumerationValues<E> {
1134 unsafe { &*(&self.0 as *const [E] as *const EnumerationValues<E>) }
1136 }
1137}
1138
1139#[repr(C)]
1150pub struct EnumerationValues<E: EnumerationValue<E>>([E]);
1151
1152impl<E: EnumerationValue<E>> Deref for EnumerationValues<E> {
1153 type Target = [E];
1154
1155 fn deref(&self) -> &Self::Target {
1158 unsafe { std::slice::from_raw_parts(self.0.as_ptr(), self.0.len() - 1) }
1160 }
1161}
1162
1163#[doc(hidden)]
1164impl<'a, E: 'a + EnumerationValue<E>> ToGlibPtr<'a, *const E::GlibType> for EnumerationValues<E> {
1165 type Storage = &'a Self;
1166
1167 fn to_glib_none(&'a self) -> Stash<'a, *const E::GlibType, Self> {
1168 Stash(self.0.as_ptr() as *const E::GlibType, self)
1169 }
1170}
1171
1172#[cfg(test)]
1173mod tests {
1174 use super::*;
1175
1176 #[test]
1177 fn test_flags() {
1178 let flags = FlagsClass::new::<crate::BindingFlags>();
1179 let values = flags.values();
1180 let def1 = values
1181 .iter()
1182 .find(|v| v.name() == "G_BINDING_DEFAULT")
1183 .unwrap();
1184 let def2 = flags.value_by_name("G_BINDING_DEFAULT").unwrap();
1185 assert!(ptr::eq(def1, def2));
1186
1187 let value = flags.to_value(0).unwrap();
1188 let values = value.get::<Vec<&FlagsValue>>().unwrap();
1189 assert_eq!(values.len(), 0);
1190
1191 assert_eq!(def1.value(), crate::BindingFlags::DEFAULT.bits());
1192 }
1193}