diff --git a/.travis.yml b/.travis.yml index bdb3c22e..c52e079a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -60,6 +60,7 @@ script: cargo clippy --release; fi - if [ "$TRAVIS_RUST_VERSION" == "nightly" ] && [ "$GTK" == "3.14" ] && ! [ "$ARM" == "1" ]; then + rustup component add rustfmt; make regen_check; fi - if ! [ "$ARM" == "1" ]; then diff --git a/Makefile b/Makefile index d60063a4..67af564d 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ GIR_FILES = gir-files/Glib-2.0.gir gir-files/GObject-2.0.gir # Run `gir` generating the bindings gir : src/auto/mod.rs src/gobject/auto/mod.rs + cargo fmt doc: $(GIR) $(GIR_FILES) $(GIR) -m doc -c Gir.toml @@ -16,6 +17,7 @@ regen_check: $(GIR) $(GIR_FILES) rm src/gobject/auto/* $(GIR) -c Gir.toml $(GIR) -c Gir_GObject.toml + cargo fmt git diff -R --exit-code src/auto/mod.rs : Gir.toml $(GIR) $(GIR_FILES) diff --git a/src/auto/checksum.rs b/src/auto/checksum.rs index 5d143b3f..a1722822 100644 --- a/src/auto/checksum.rs +++ b/src/auto/checksum.rs @@ -2,9 +2,9 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT -use ChecksumType; use glib_sys; use translate::*; +use ChecksumType; glib_wrapper! { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -19,9 +19,7 @@ glib_wrapper! { impl Checksum { pub fn new(checksum_type: ChecksumType) -> Checksum { - unsafe { - from_glib_full(glib_sys::g_checksum_new(checksum_type.to_glib())) - } + unsafe { from_glib_full(glib_sys::g_checksum_new(checksum_type.to_glib())) } } pub fn reset(&mut self) { @@ -38,9 +36,7 @@ impl Checksum { } pub fn type_get_length(checksum_type: ChecksumType) -> isize { - unsafe { - glib_sys::g_checksum_type_get_length(checksum_type.to_glib()) - } + unsafe { glib_sys::g_checksum_type_get_length(checksum_type.to_glib()) } } } diff --git a/src/auto/constants.rs b/src/auto/constants.rs index 63951b22..5b8567b1 100644 --- a/src/auto/constants.rs +++ b/src/auto/constants.rs @@ -6,110 +6,242 @@ use glib_sys; use std::ffi::CStr; lazy_static! { - pub static ref CSET_A_2_Z: &'static str = unsafe{CStr::from_ptr(glib_sys::G_CSET_A_2_Z).to_str().unwrap()}; + pub static ref CSET_A_2_Z: &'static str = + unsafe { CStr::from_ptr(glib_sys::G_CSET_A_2_Z).to_str().unwrap() }; } lazy_static! { - pub static ref CSET_DIGITS: &'static str = unsafe{CStr::from_ptr(glib_sys::G_CSET_DIGITS).to_str().unwrap()}; + pub static ref CSET_DIGITS: &'static str = + unsafe { CStr::from_ptr(glib_sys::G_CSET_DIGITS).to_str().unwrap() }; } lazy_static! { - pub static ref CSET_a_2_z: &'static str = unsafe{CStr::from_ptr(glib_sys::G_CSET_a_2_z).to_str().unwrap()}; + pub static ref CSET_a_2_z: &'static str = + unsafe { CStr::from_ptr(glib_sys::G_CSET_a_2_z).to_str().unwrap() }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_ACTION_GROUP_PREFIX: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_ACTION_GROUP_PREFIX).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_ACTION_GROUP_PREFIX: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_ACTION_GROUP_PREFIX) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_GROUP: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_GROUP).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_GROUP: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_GROUP) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_ACTIONS: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_ACTIONS).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_ACTIONS: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_ACTIONS) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_CATEGORIES: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_CATEGORIES).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_CATEGORIES: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_CATEGORIES) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_COMMENT: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_COMMENT).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_COMMENT: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_COMMENT) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_EXEC: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_EXEC).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_EXEC: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_EXEC) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_FULLNAME: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_FULLNAME).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_FULLNAME: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_FULLNAME) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_GENERIC_NAME: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_GENERIC_NAME: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_GETTEXT_DOMAIN) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_HIDDEN: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_HIDDEN).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_HIDDEN: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_HIDDEN) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_ICON: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_ICON).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_ICON: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_ICON) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_KEYWORDS: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_KEYWORDS).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_KEYWORDS: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_KEYWORDS) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_MIME_TYPE: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_MIME_TYPE).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_MIME_TYPE: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_MIME_TYPE) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_NAME: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_NAME).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_NAME: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_NAME) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_NO_DISPLAY: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_NO_DISPLAY: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_PATH: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_PATH).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_PATH: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_PATH) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_TERMINAL: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_TERMINAL).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_TERMINAL: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_TERMINAL) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_TRY_EXEC: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_TRY_EXEC).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_TRY_EXEC: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_TRY_EXEC) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_TYPE: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_TYPE).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_TYPE: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_TYPE) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_URL: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_URL).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_URL: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_URL) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_KEY_VERSION: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_VERSION).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_KEY_VERSION: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_KEY_VERSION) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_TYPE_APPLICATION: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_TYPE_APPLICATION).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_TYPE_APPLICATION: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_TYPE_APPLICATION) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_TYPE_DIRECTORY: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_TYPE_DIRECTORY).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_TYPE_DIRECTORY: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_TYPE_DIRECTORY) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref KEY_FILE_DESKTOP_TYPE_LINK: &'static str = unsafe{CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_TYPE_LINK).to_str().unwrap()}; + pub static ref KEY_FILE_DESKTOP_TYPE_LINK: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_KEY_FILE_DESKTOP_TYPE_LINK) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref OPTION_REMAINING: &'static str = unsafe{CStr::from_ptr(glib_sys::G_OPTION_REMAINING).to_str().unwrap()}; + pub static ref OPTION_REMAINING: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_OPTION_REMAINING) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref STR_DELIMITERS: &'static str = unsafe{CStr::from_ptr(glib_sys::G_STR_DELIMITERS).to_str().unwrap()}; + pub static ref STR_DELIMITERS: &'static str = + unsafe { CStr::from_ptr(glib_sys::G_STR_DELIMITERS).to_str().unwrap() }; } lazy_static! { - pub static ref URI_RESERVED_CHARS_GENERIC_DELIMITERS: &'static str = unsafe{CStr::from_ptr(glib_sys::G_URI_RESERVED_CHARS_GENERIC_DELIMITERS).to_str().unwrap()}; + pub static ref URI_RESERVED_CHARS_GENERIC_DELIMITERS: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_URI_RESERVED_CHARS_GENERIC_DELIMITERS) + .to_str() + .unwrap() + }; } lazy_static! { - pub static ref URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS: &'static str = unsafe{CStr::from_ptr(glib_sys::G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS).to_str().unwrap()}; + pub static ref URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS: &'static str = unsafe { + CStr::from_ptr(glib_sys::G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS) + .to_str() + .unwrap() + }; } diff --git a/src/auto/date_time.rs b/src/auto/date_time.rs index d092b111..af316d54 100644 --- a/src/auto/date_time.rs +++ b/src/auto/date_time.rs @@ -2,14 +2,14 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT -use GString; -use TimeSpan; -use TimeZone; use glib_sys; use std::cmp; use std::hash; use std::mem; use translate::*; +use GString; +use TimeSpan; +use TimeZone; glib_wrapper! { #[derive(Debug)] @@ -23,16 +23,35 @@ glib_wrapper! { } impl DateTime { - pub fn new(tz: &TimeZone, year: i32, month: i32, day: i32, hour: i32, minute: i32, seconds: f64) -> DateTime { - unsafe { - from_glib_full(glib_sys::g_date_time_new(tz.to_glib_none().0, year, month, day, hour, minute, seconds)) + pub fn new( + tz: &TimeZone, + year: i32, + month: i32, + day: i32, + hour: i32, + minute: i32, + seconds: f64, + ) -> DateTime { + unsafe { + from_glib_full(glib_sys::g_date_time_new( + tz.to_glib_none().0, + year, + month, + day, + hour, + minute, + seconds, + )) } } #[cfg(any(feature = "v2_56", feature = "dox"))] pub fn new_from_iso8601(text: &str, default_tz: Option<&TimeZone>) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_new_from_iso8601(text.to_glib_none().0, default_tz.to_glib_none().0)) + from_glib_full(glib_sys::g_date_time_new_from_iso8601( + text.to_glib_none().0, + default_tz.to_glib_none().0, + )) } } @@ -45,202 +64,215 @@ impl DateTime { //} pub fn new_from_unix_local(t: i64) -> DateTime { - unsafe { - from_glib_full(glib_sys::g_date_time_new_from_unix_local(t)) - } + unsafe { from_glib_full(glib_sys::g_date_time_new_from_unix_local(t)) } } pub fn new_from_unix_utc(t: i64) -> DateTime { - unsafe { - from_glib_full(glib_sys::g_date_time_new_from_unix_utc(t)) - } + unsafe { from_glib_full(glib_sys::g_date_time_new_from_unix_utc(t)) } } - pub fn new_local(year: i32, month: i32, day: i32, hour: i32, minute: i32, seconds: f64) -> DateTime { + pub fn new_local( + year: i32, + month: i32, + day: i32, + hour: i32, + minute: i32, + seconds: f64, + ) -> DateTime { unsafe { - from_glib_full(glib_sys::g_date_time_new_local(year, month, day, hour, minute, seconds)) + from_glib_full(glib_sys::g_date_time_new_local( + year, month, day, hour, minute, seconds, + )) } } pub fn new_now(tz: &TimeZone) -> DateTime { - unsafe { - from_glib_full(glib_sys::g_date_time_new_now(tz.to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_date_time_new_now(tz.to_glib_none().0)) } } pub fn new_now_local() -> DateTime { - unsafe { - from_glib_full(glib_sys::g_date_time_new_now_local()) - } + unsafe { from_glib_full(glib_sys::g_date_time_new_now_local()) } } pub fn new_now_utc() -> DateTime { - unsafe { - from_glib_full(glib_sys::g_date_time_new_now_utc()) - } + unsafe { from_glib_full(glib_sys::g_date_time_new_now_utc()) } } - pub fn new_utc(year: i32, month: i32, day: i32, hour: i32, minute: i32, seconds: f64) -> DateTime { + pub fn new_utc( + year: i32, + month: i32, + day: i32, + hour: i32, + minute: i32, + seconds: f64, + ) -> DateTime { unsafe { - from_glib_full(glib_sys::g_date_time_new_utc(year, month, day, hour, minute, seconds)) + from_glib_full(glib_sys::g_date_time_new_utc( + year, month, day, hour, minute, seconds, + )) } } pub fn add(&self, timespan: TimeSpan) -> Option { - unsafe { - from_glib_full(glib_sys::g_date_time_add(self.to_glib_none().0, timespan)) - } + unsafe { from_glib_full(glib_sys::g_date_time_add(self.to_glib_none().0, timespan)) } } pub fn add_days(&self, days: i32) -> Option { - unsafe { - from_glib_full(glib_sys::g_date_time_add_days(self.to_glib_none().0, days)) - } + unsafe { from_glib_full(glib_sys::g_date_time_add_days(self.to_glib_none().0, days)) } } - pub fn add_full(&self, years: i32, months: i32, days: i32, hours: i32, minutes: i32, seconds: f64) -> Option { + pub fn add_full( + &self, + years: i32, + months: i32, + days: i32, + hours: i32, + minutes: i32, + seconds: f64, + ) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_add_full(self.to_glib_none().0, years, months, days, hours, minutes, seconds)) + from_glib_full(glib_sys::g_date_time_add_full( + self.to_glib_none().0, + years, + months, + days, + hours, + minutes, + seconds, + )) } } pub fn add_hours(&self, hours: i32) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_add_hours(self.to_glib_none().0, hours)) + from_glib_full(glib_sys::g_date_time_add_hours( + self.to_glib_none().0, + hours, + )) } } pub fn add_minutes(&self, minutes: i32) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_add_minutes(self.to_glib_none().0, minutes)) + from_glib_full(glib_sys::g_date_time_add_minutes( + self.to_glib_none().0, + minutes, + )) } } pub fn add_months(&self, months: i32) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_add_months(self.to_glib_none().0, months)) + from_glib_full(glib_sys::g_date_time_add_months( + self.to_glib_none().0, + months, + )) } } pub fn add_seconds(&self, seconds: f64) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_add_seconds(self.to_glib_none().0, seconds)) + from_glib_full(glib_sys::g_date_time_add_seconds( + self.to_glib_none().0, + seconds, + )) } } pub fn add_weeks(&self, weeks: i32) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_add_weeks(self.to_glib_none().0, weeks)) + from_glib_full(glib_sys::g_date_time_add_weeks( + self.to_glib_none().0, + weeks, + )) } } pub fn add_years(&self, years: i32) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_add_years(self.to_glib_none().0, years)) + from_glib_full(glib_sys::g_date_time_add_years( + self.to_glib_none().0, + years, + )) } } pub fn difference(&self, begin: &DateTime) -> TimeSpan { - unsafe { - glib_sys::g_date_time_difference(self.to_glib_none().0, begin.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_difference(self.to_glib_none().0, begin.to_glib_none().0) } } pub fn format(&self, format: &str) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_format(self.to_glib_none().0, format.to_glib_none().0)) + from_glib_full(glib_sys::g_date_time_format( + self.to_glib_none().0, + format.to_glib_none().0, + )) } } pub fn get_day_of_month(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_day_of_month(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_day_of_month(self.to_glib_none().0) } } pub fn get_day_of_week(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_day_of_week(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_day_of_week(self.to_glib_none().0) } } pub fn get_day_of_year(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_day_of_year(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_day_of_year(self.to_glib_none().0) } } pub fn get_hour(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_hour(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_hour(self.to_glib_none().0) } } pub fn get_microsecond(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_microsecond(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_microsecond(self.to_glib_none().0) } } pub fn get_minute(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_minute(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_minute(self.to_glib_none().0) } } pub fn get_month(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_month(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_month(self.to_glib_none().0) } } pub fn get_second(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_second(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_second(self.to_glib_none().0) } } pub fn get_seconds(&self) -> f64 { - unsafe { - glib_sys::g_date_time_get_seconds(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_seconds(self.to_glib_none().0) } } #[cfg(any(feature = "v2_58", feature = "dox"))] pub fn get_timezone(&self) -> Option { - unsafe { - from_glib_none(glib_sys::g_date_time_get_timezone(self.to_glib_none().0)) - } + unsafe { from_glib_none(glib_sys::g_date_time_get_timezone(self.to_glib_none().0)) } } pub fn get_timezone_abbreviation(&self) -> Option { unsafe { - from_glib_none(glib_sys::g_date_time_get_timezone_abbreviation(self.to_glib_none().0)) + from_glib_none(glib_sys::g_date_time_get_timezone_abbreviation( + self.to_glib_none().0, + )) } } pub fn get_utc_offset(&self) -> TimeSpan { - unsafe { - glib_sys::g_date_time_get_utc_offset(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_utc_offset(self.to_glib_none().0) } } pub fn get_week_numbering_year(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_week_numbering_year(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_week_numbering_year(self.to_glib_none().0) } } pub fn get_week_of_year(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_week_of_year(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_week_of_year(self.to_glib_none().0) } } pub fn get_year(&self) -> i32 { - unsafe { - glib_sys::g_date_time_get_year(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_get_year(self.to_glib_none().0) } } pub fn get_ymd(&self) -> (i32, i32, i32) { @@ -255,14 +287,14 @@ impl DateTime { pub fn is_daylight_savings(&self) -> bool { unsafe { - from_glib(glib_sys::g_date_time_is_daylight_savings(self.to_glib_none().0)) + from_glib(glib_sys::g_date_time_is_daylight_savings( + self.to_glib_none().0, + )) } } pub fn to_local(&self) -> Option { - unsafe { - from_glib_full(glib_sys::g_date_time_to_local(self.to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_date_time_to_local(self.to_glib_none().0)) } } //pub fn to_timeval(&self, tv: /*Ignored*/&mut TimeVal) -> bool { @@ -271,37 +303,49 @@ impl DateTime { pub fn to_timezone(&self, tz: &TimeZone) -> Option { unsafe { - from_glib_full(glib_sys::g_date_time_to_timezone(self.to_glib_none().0, tz.to_glib_none().0)) + from_glib_full(glib_sys::g_date_time_to_timezone( + self.to_glib_none().0, + tz.to_glib_none().0, + )) } } pub fn to_unix(&self) -> i64 { - unsafe { - glib_sys::g_date_time_to_unix(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_time_to_unix(self.to_glib_none().0) } } pub fn to_utc(&self) -> Option { - unsafe { - from_glib_full(glib_sys::g_date_time_to_utc(self.to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_date_time_to_utc(self.to_glib_none().0)) } } fn compare(&self, dt2: &DateTime) -> i32 { unsafe { - glib_sys::g_date_time_compare(ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(self).0 as glib_sys::gconstpointer, ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(dt2).0 as glib_sys::gconstpointer) + glib_sys::g_date_time_compare( + ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(self).0 + as glib_sys::gconstpointer, + ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(dt2).0 + as glib_sys::gconstpointer, + ) } } fn equal(&self, dt2: &DateTime) -> bool { unsafe { - from_glib(glib_sys::g_date_time_equal(ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(self).0 as glib_sys::gconstpointer, ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(dt2).0 as glib_sys::gconstpointer)) + from_glib(glib_sys::g_date_time_equal( + ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(self).0 + as glib_sys::gconstpointer, + ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(dt2).0 + as glib_sys::gconstpointer, + )) } } fn hash(&self) -> u32 { unsafe { - glib_sys::g_date_time_hash(ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(self).0 as glib_sys::gconstpointer) + glib_sys::g_date_time_hash( + ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(self).0 + as glib_sys::gconstpointer, + ) } } } @@ -331,7 +375,10 @@ impl Eq for DateTime {} impl hash::Hash for DateTime { #[inline] - fn hash(&self, state: &mut H) where H: hash::Hasher { + fn hash(&self, state: &mut H) + where + H: hash::Hasher, + { hash::Hash::hash(&self.hash(), state) } } diff --git a/src/auto/enums.rs b/src/auto/enums.rs index 2485fe68..404f74f5 100644 --- a/src/auto/enums.rs +++ b/src/auto/enums.rs @@ -2,14 +2,13 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT -use Quark; use error::ErrorDomain; use glib_sys; use std::fmt; use translate::*; +use Quark; -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -#[derive(Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] pub enum ChecksumType { Md5, Sha1, @@ -23,15 +22,19 @@ pub enum ChecksumType { impl fmt::Display for ChecksumType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "ChecksumType::{}", match *self { - ChecksumType::Md5 => "Md5", - ChecksumType::Sha1 => "Sha1", - ChecksumType::Sha256 => "Sha256", - ChecksumType::Sha512 => "Sha512", - #[cfg(any(feature = "v2_52", feature = "dox"))] - ChecksumType::Sha384 => "Sha384", - _ => "Unknown", - }) + write!( + f, + "ChecksumType::{}", + match *self { + ChecksumType::Md5 => "Md5", + ChecksumType::Sha1 => "Sha1", + ChecksumType::Sha256 => "Sha256", + ChecksumType::Sha512 => "Sha512", + #[cfg(any(feature = "v2_52", feature = "dox"))] + ChecksumType::Sha384 => "Sha384", + _ => "Unknown", + } + ) } } @@ -47,7 +50,7 @@ impl ToGlib for ChecksumType { ChecksumType::Sha512 => glib_sys::G_CHECKSUM_SHA512, #[cfg(any(feature = "v2_52", feature = "dox"))] ChecksumType::Sha384 => glib_sys::G_CHECKSUM_SHA384, - ChecksumType::__Unknown(value) => value + ChecksumType::__Unknown(value) => value, } } } @@ -67,8 +70,7 @@ impl FromGlib for ChecksumType { } } -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -#[derive(Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] pub enum DateMonth { BadMonth, January, @@ -89,22 +91,26 @@ pub enum DateMonth { impl fmt::Display for DateMonth { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "DateMonth::{}", match *self { - DateMonth::BadMonth => "BadMonth", - DateMonth::January => "January", - DateMonth::February => "February", - DateMonth::March => "March", - DateMonth::April => "April", - DateMonth::May => "May", - DateMonth::June => "June", - DateMonth::July => "July", - DateMonth::August => "August", - DateMonth::September => "September", - DateMonth::October => "October", - DateMonth::November => "November", - DateMonth::December => "December", - _ => "Unknown", - }) + write!( + f, + "DateMonth::{}", + match *self { + DateMonth::BadMonth => "BadMonth", + DateMonth::January => "January", + DateMonth::February => "February", + DateMonth::March => "March", + DateMonth::April => "April", + DateMonth::May => "May", + DateMonth::June => "June", + DateMonth::July => "July", + DateMonth::August => "August", + DateMonth::September => "September", + DateMonth::October => "October", + DateMonth::November => "November", + DateMonth::December => "December", + _ => "Unknown", + } + ) } } @@ -127,7 +133,7 @@ impl ToGlib for DateMonth { DateMonth::October => glib_sys::G_DATE_OCTOBER, DateMonth::November => glib_sys::G_DATE_NOVEMBER, DateMonth::December => glib_sys::G_DATE_DECEMBER, - DateMonth::__Unknown(value) => value + DateMonth::__Unknown(value) => value, } } } @@ -154,8 +160,7 @@ impl FromGlib for DateMonth { } } -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -#[derive(Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] pub enum DateWeekday { BadWeekday, Monday, @@ -171,17 +176,21 @@ pub enum DateWeekday { impl fmt::Display for DateWeekday { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "DateWeekday::{}", match *self { - DateWeekday::BadWeekday => "BadWeekday", - DateWeekday::Monday => "Monday", - DateWeekday::Tuesday => "Tuesday", - DateWeekday::Wednesday => "Wednesday", - DateWeekday::Thursday => "Thursday", - DateWeekday::Friday => "Friday", - DateWeekday::Saturday => "Saturday", - DateWeekday::Sunday => "Sunday", - _ => "Unknown", - }) + write!( + f, + "DateWeekday::{}", + match *self { + DateWeekday::BadWeekday => "BadWeekday", + DateWeekday::Monday => "Monday", + DateWeekday::Tuesday => "Tuesday", + DateWeekday::Wednesday => "Wednesday", + DateWeekday::Thursday => "Thursday", + DateWeekday::Friday => "Friday", + DateWeekday::Saturday => "Saturday", + DateWeekday::Sunday => "Sunday", + _ => "Unknown", + } + ) } } @@ -199,7 +208,7 @@ impl ToGlib for DateWeekday { DateWeekday::Friday => glib_sys::G_DATE_FRIDAY, DateWeekday::Saturday => glib_sys::G_DATE_SATURDAY, DateWeekday::Sunday => glib_sys::G_DATE_SUNDAY, - DateWeekday::__Unknown(value) => value + DateWeekday::__Unknown(value) => value, } } } @@ -221,8 +230,7 @@ impl FromGlib for DateWeekday { } } -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -#[derive(Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] pub enum KeyFileError { UnknownEncoding, Parse, @@ -236,15 +244,19 @@ pub enum KeyFileError { impl fmt::Display for KeyFileError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "KeyFileError::{}", match *self { - KeyFileError::UnknownEncoding => "UnknownEncoding", - KeyFileError::Parse => "Parse", - KeyFileError::NotFound => "NotFound", - KeyFileError::KeyNotFound => "KeyNotFound", - KeyFileError::GroupNotFound => "GroupNotFound", - KeyFileError::InvalidValue => "InvalidValue", - _ => "Unknown", - }) + write!( + f, + "KeyFileError::{}", + match *self { + KeyFileError::UnknownEncoding => "UnknownEncoding", + KeyFileError::Parse => "Parse", + KeyFileError::NotFound => "NotFound", + KeyFileError::KeyNotFound => "KeyNotFound", + KeyFileError::GroupNotFound => "GroupNotFound", + KeyFileError::InvalidValue => "InvalidValue", + _ => "Unknown", + } + ) } } @@ -260,7 +272,7 @@ impl ToGlib for KeyFileError { KeyFileError::KeyNotFound => glib_sys::G_KEY_FILE_ERROR_KEY_NOT_FOUND, KeyFileError::GroupNotFound => glib_sys::G_KEY_FILE_ERROR_GROUP_NOT_FOUND, KeyFileError::InvalidValue => glib_sys::G_KEY_FILE_ERROR_INVALID_VALUE, - KeyFileError::__Unknown(value) => value + KeyFileError::__Unknown(value) => value, } } } @@ -302,8 +314,7 @@ impl ErrorDomain for KeyFileError { } } -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -#[derive(Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] pub enum OptionArg { None, String, @@ -320,18 +331,22 @@ pub enum OptionArg { impl fmt::Display for OptionArg { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "OptionArg::{}", match *self { - OptionArg::None => "None", - OptionArg::String => "String", - OptionArg::Int => "Int", - OptionArg::Callback => "Callback", - OptionArg::Filename => "Filename", - OptionArg::StringArray => "StringArray", - OptionArg::FilenameArray => "FilenameArray", - OptionArg::Double => "Double", - OptionArg::Int64 => "Int64", - _ => "Unknown", - }) + write!( + f, + "OptionArg::{}", + match *self { + OptionArg::None => "None", + OptionArg::String => "String", + OptionArg::Int => "Int", + OptionArg::Callback => "Callback", + OptionArg::Filename => "Filename", + OptionArg::StringArray => "StringArray", + OptionArg::FilenameArray => "FilenameArray", + OptionArg::Double => "Double", + OptionArg::Int64 => "Int64", + _ => "Unknown", + } + ) } } @@ -350,7 +365,7 @@ impl ToGlib for OptionArg { OptionArg::FilenameArray => glib_sys::G_OPTION_ARG_FILENAME_ARRAY, OptionArg::Double => glib_sys::G_OPTION_ARG_DOUBLE, OptionArg::Int64 => glib_sys::G_OPTION_ARG_INT64, - OptionArg::__Unknown(value) => value + OptionArg::__Unknown(value) => value, } } } @@ -373,8 +388,7 @@ impl FromGlib for OptionArg { } } -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -#[derive(Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] pub enum SeekType { Cur, Set, @@ -385,12 +399,16 @@ pub enum SeekType { impl fmt::Display for SeekType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "SeekType::{}", match *self { - SeekType::Cur => "Cur", - SeekType::Set => "Set", - SeekType::End => "End", - _ => "Unknown", - }) + write!( + f, + "SeekType::{}", + match *self { + SeekType::Cur => "Cur", + SeekType::Set => "Set", + SeekType::End => "End", + _ => "Unknown", + } + ) } } @@ -403,7 +421,7 @@ impl ToGlib for SeekType { SeekType::Cur => glib_sys::G_SEEK_CUR, SeekType::Set => glib_sys::G_SEEK_SET, SeekType::End => glib_sys::G_SEEK_END, - SeekType::__Unknown(value) => value + SeekType::__Unknown(value) => value, } } } @@ -420,8 +438,7 @@ impl FromGlib for SeekType { } } -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -#[derive(Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] pub enum TimeType { Standard, Daylight, @@ -432,12 +449,16 @@ pub enum TimeType { impl fmt::Display for TimeType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "TimeType::{}", match *self { - TimeType::Standard => "Standard", - TimeType::Daylight => "Daylight", - TimeType::Universal => "Universal", - _ => "Unknown", - }) + write!( + f, + "TimeType::{}", + match *self { + TimeType::Standard => "Standard", + TimeType::Daylight => "Daylight", + TimeType::Universal => "Universal", + _ => "Unknown", + } + ) } } @@ -450,7 +471,7 @@ impl ToGlib for TimeType { TimeType::Standard => glib_sys::G_TIME_TYPE_STANDARD, TimeType::Daylight => glib_sys::G_TIME_TYPE_DAYLIGHT, TimeType::Universal => glib_sys::G_TIME_TYPE_UNIVERSAL, - TimeType::__Unknown(value) => value + TimeType::__Unknown(value) => value, } } } @@ -466,4 +487,3 @@ impl FromGlib for TimeType { } } } - diff --git a/src/auto/flags.rs b/src/auto/flags.rs index 2c8494b3..19193c8c 100644 --- a/src/auto/flags.rs +++ b/src/auto/flags.rs @@ -2,8 +2,6 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT -use StaticType; -use Type; use glib_sys; use gobject_sys; use translate::*; @@ -11,6 +9,8 @@ use value::FromValue; use value::FromValueOptional; use value::SetValue; use value::Value; +use StaticType; +use Type; bitflags! { pub struct FileTest: u32 { @@ -197,4 +197,3 @@ impl FromGlib for SpawnFlags { SpawnFlags::from_bits_truncate(value) } } - diff --git a/src/auto/functions.rs b/src/auto/functions.rs index 0abd72c6..aa1a2d4b 100644 --- a/src/auto/functions.rs +++ b/src/auto/functions.rs @@ -2,6 +2,13 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT +use glib_sys; +use libc; +use std; +use std::mem; +use std::ptr; +use translate::*; +use types; use Bytes; use ChecksumType; use Error; @@ -10,30 +17,38 @@ use FormatSizeFlags; use GString; use Source; use UserDirectory; -use glib_sys; -use libc; -use std; -use std::mem; -use std::ptr; -use translate::*; -use types; - pub fn access>(filename: P, mode: i32) -> i32 { - unsafe { - glib_sys::g_access(filename.as_ref().to_glib_none().0, mode) - } + unsafe { glib_sys::g_access(filename.as_ref().to_glib_none().0, mode) } } -pub fn assert_warning(log_domain: &str, file: &str, line: i32, pretty_function: &str, expression: &str) { +pub fn assert_warning( + log_domain: &str, + file: &str, + line: i32, + pretty_function: &str, + expression: &str, +) { unsafe { - glib_sys::g_assert_warning(log_domain.to_glib_none().0, file.to_glib_none().0, line, pretty_function.to_glib_none().0, expression.to_glib_none().0); + glib_sys::g_assert_warning( + log_domain.to_glib_none().0, + file.to_glib_none().0, + line, + pretty_function.to_glib_none().0, + expression.to_glib_none().0, + ); } } pub fn assertion_message(domain: &str, file: &str, line: i32, func: &str, message: &str) { unsafe { - glib_sys::g_assertion_message(domain.to_glib_none().0, file.to_glib_none().0, line, func.to_glib_none().0, message.to_glib_none().0); + glib_sys::g_assertion_message( + domain.to_glib_none().0, + file.to_glib_none().0, + line, + func.to_glib_none().0, + message.to_glib_none().0, + ); } } @@ -41,16 +56,37 @@ pub fn assertion_message(domain: &str, file: &str, line: i32, func: &str, messag // unsafe { TODO: call glib_sys:g_assertion_message_cmpnum() } //} -pub fn assertion_message_cmpstr(domain: &str, file: &str, line: i32, func: &str, expr: &str, arg1: &str, cmp: &str, arg2: &str) { +pub fn assertion_message_cmpstr( + domain: &str, + file: &str, + line: i32, + func: &str, + expr: &str, + arg1: &str, + cmp: &str, + arg2: &str, +) { unsafe { - glib_sys::g_assertion_message_cmpstr(domain.to_glib_none().0, file.to_glib_none().0, line, func.to_glib_none().0, expr.to_glib_none().0, arg1.to_glib_none().0, cmp.to_glib_none().0, arg2.to_glib_none().0); + glib_sys::g_assertion_message_cmpstr( + domain.to_glib_none().0, + file.to_glib_none().0, + line, + func.to_glib_none().0, + expr.to_glib_none().0, + arg1.to_glib_none().0, + cmp.to_glib_none().0, + arg2.to_glib_none().0, + ); } } pub fn base64_decode(text: &str) -> Vec { unsafe { let mut out_len = mem::uninitialized(); - let ret = FromGlibContainer::from_glib_full_num(glib_sys::g_base64_decode(text.to_glib_none().0, &mut out_len), out_len as usize); + let ret = FromGlibContainer::from_glib_full_num( + glib_sys::g_base64_decode(text.to_glib_none().0, &mut out_len), + out_len as usize, + ); ret } } @@ -65,9 +101,7 @@ pub fn base64_decode(text: &str) -> Vec { pub fn base64_encode(data: &[u8]) -> Option { let len = data.len() as usize; - unsafe { - from_glib_full(glib_sys::g_base64_encode(data.to_glib_none().0, len)) - } + unsafe { from_glib_full(glib_sys::g_base64_encode(data.to_glib_none().0, len)) } } //pub fn base64_encode_close(break_lines: bool, out: Vec, state: &mut i32, save: &mut i32) -> usize { @@ -79,21 +113,15 @@ pub fn base64_encode(data: &[u8]) -> Option { //} pub fn bit_nth_lsf(mask: libc::c_ulong, nth_bit: i32) -> i32 { - unsafe { - glib_sys::g_bit_nth_lsf(mask, nth_bit) - } + unsafe { glib_sys::g_bit_nth_lsf(mask, nth_bit) } } pub fn bit_nth_msf(mask: libc::c_ulong, nth_bit: i32) -> i32 { - unsafe { - glib_sys::g_bit_nth_msf(mask, nth_bit) - } + unsafe { glib_sys::g_bit_nth_msf(mask, nth_bit) } } pub fn bit_storage(number: libc::c_ulong) -> u32 { - unsafe { - glib_sys::g_bit_storage(number) - } + unsafe { glib_sys::g_bit_storage(number) } } //pub fn build_filename>(first_element: P, : /*Unknown conversion*//*Unimplemented*/Fundamental: VarArgs) -> Option { @@ -106,9 +134,7 @@ pub fn bit_storage(number: libc::c_ulong) -> u32 { //} pub fn build_filenamev(args: &[&std::path::Path]) -> Option { - unsafe { - from_glib_full(glib_sys::g_build_filenamev(args.to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_build_filenamev(args.to_glib_none().0)) } } //pub fn build_path, Q: AsRef>(separator: P, first_element: Q, : /*Unknown conversion*//*Unimplemented*/Fundamental: VarArgs) -> Option { @@ -117,26 +143,41 @@ pub fn build_filenamev(args: &[&std::path::Path]) -> Option pub fn build_pathv(separator: &str, args: &[&std::path::Path]) -> Option { unsafe { - from_glib_full(glib_sys::g_build_pathv(separator.to_glib_none().0, args.to_glib_none().0)) + from_glib_full(glib_sys::g_build_pathv( + separator.to_glib_none().0, + args.to_glib_none().0, + )) } } #[cfg(any(feature = "v2_58", feature = "dox"))] -pub fn canonicalize_filename, Q: AsRef>(filename: P, relative_to: Q) -> Option { +pub fn canonicalize_filename, Q: AsRef>( + filename: P, + relative_to: Q, +) -> Option { unsafe { - from_glib_full(glib_sys::g_canonicalize_filename(filename.as_ref().to_glib_none().0, relative_to.as_ref().to_glib_none().0)) + from_glib_full(glib_sys::g_canonicalize_filename( + filename.as_ref().to_glib_none().0, + relative_to.as_ref().to_glib_none().0, + )) } } pub fn chdir>(path: P) -> i32 { - unsafe { - glib_sys::g_chdir(path.as_ref().to_glib_none().0) - } + unsafe { glib_sys::g_chdir(path.as_ref().to_glib_none().0) } } -pub fn check_version(required_major: u32, required_minor: u32, required_micro: u32) -> Option { +pub fn check_version( + required_major: u32, + required_minor: u32, + required_micro: u32, +) -> Option { unsafe { - from_glib_none(glib_sys::glib_check_version(required_major, required_minor, required_micro)) + from_glib_none(glib_sys::glib_check_version( + required_major, + required_minor, + required_micro, + )) } } @@ -144,7 +185,11 @@ pub fn clear_error() -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); let _ = glib_sys::g_clear_error(&mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } @@ -159,44 +204,83 @@ pub fn clear_error() -> Result<(), Error> { pub fn compute_checksum_for_bytes(checksum_type: ChecksumType, data: &Bytes) -> Option { unsafe { - from_glib_full(glib_sys::g_compute_checksum_for_bytes(checksum_type.to_glib(), data.to_glib_none().0)) + from_glib_full(glib_sys::g_compute_checksum_for_bytes( + checksum_type.to_glib(), + data.to_glib_none().0, + )) } } pub fn compute_checksum_for_data(checksum_type: ChecksumType, data: &[u8]) -> Option { let length = data.len() as usize; unsafe { - from_glib_full(glib_sys::g_compute_checksum_for_data(checksum_type.to_glib(), data.to_glib_none().0, length)) + from_glib_full(glib_sys::g_compute_checksum_for_data( + checksum_type.to_glib(), + data.to_glib_none().0, + length, + )) } } pub fn compute_checksum_for_string(checksum_type: ChecksumType, str: &str) -> Option { let length = str.len() as isize; unsafe { - from_glib_full(glib_sys::g_compute_checksum_for_string(checksum_type.to_glib(), str.to_glib_none().0, length)) + from_glib_full(glib_sys::g_compute_checksum_for_string( + checksum_type.to_glib(), + str.to_glib_none().0, + length, + )) } } #[cfg(any(feature = "v2_50", feature = "dox"))] -pub fn compute_hmac_for_bytes(digest_type: ChecksumType, key: &Bytes, data: &Bytes) -> Option { +pub fn compute_hmac_for_bytes( + digest_type: ChecksumType, + key: &Bytes, + data: &Bytes, +) -> Option { unsafe { - from_glib_full(glib_sys::g_compute_hmac_for_bytes(digest_type.to_glib(), key.to_glib_none().0, data.to_glib_none().0)) + from_glib_full(glib_sys::g_compute_hmac_for_bytes( + digest_type.to_glib(), + key.to_glib_none().0, + data.to_glib_none().0, + )) } } -pub fn compute_hmac_for_data(digest_type: ChecksumType, key: &[u8], data: &[u8]) -> Option { +pub fn compute_hmac_for_data( + digest_type: ChecksumType, + key: &[u8], + data: &[u8], +) -> Option { let key_len = key.len() as usize; let length = data.len() as usize; unsafe { - from_glib_full(glib_sys::g_compute_hmac_for_data(digest_type.to_glib(), key.to_glib_none().0, key_len, data.to_glib_none().0, length)) + from_glib_full(glib_sys::g_compute_hmac_for_data( + digest_type.to_glib(), + key.to_glib_none().0, + key_len, + data.to_glib_none().0, + length, + )) } } -pub fn compute_hmac_for_string(digest_type: ChecksumType, key: &[u8], str: &str) -> Option { +pub fn compute_hmac_for_string( + digest_type: ChecksumType, + key: &[u8], + str: &str, +) -> Option { let key_len = key.len() as usize; let length = str.len() as isize; unsafe { - from_glib_full(glib_sys::g_compute_hmac_for_string(digest_type.to_glib(), key.to_glib_none().0, key_len, str.to_glib_none().0, length)) + from_glib_full(glib_sys::g_compute_hmac_for_string( + digest_type.to_glib(), + key.to_glib_none().0, + key_len, + str.to_glib_none().0, + length, + )) } } @@ -274,13 +358,20 @@ pub fn compute_hmac_for_string(digest_type: ChecksumType, key: &[u8], str: &str) pub fn dcgettext(domain: Option<&str>, msgid: &str, category: i32) -> Option { unsafe { - from_glib_none(glib_sys::g_dcgettext(domain.to_glib_none().0, msgid.to_glib_none().0, category)) + from_glib_none(glib_sys::g_dcgettext( + domain.to_glib_none().0, + msgid.to_glib_none().0, + category, + )) } } pub fn dgettext(domain: Option<&str>, msgid: &str) -> Option { unsafe { - from_glib_none(glib_sys::g_dgettext(domain.to_glib_none().0, msgid.to_glib_none().0)) + from_glib_none(glib_sys::g_dgettext( + domain.to_glib_none().0, + msgid.to_glib_none().0, + )) } } @@ -292,9 +383,19 @@ pub fn dgettext(domain: Option<&str>, msgid: &str) -> Option { // unsafe { TODO: call glib_sys:g_direct_hash() } //} -pub fn dngettext(domain: Option<&str>, msgid: &str, msgid_plural: &str, n: libc::c_ulong) -> Option { +pub fn dngettext( + domain: Option<&str>, + msgid: &str, + msgid_plural: &str, + n: libc::c_ulong, +) -> Option { unsafe { - from_glib_none(glib_sys::g_dngettext(domain.to_glib_none().0, msgid.to_glib_none().0, msgid_plural.to_glib_none().0, n)) + from_glib_none(glib_sys::g_dngettext( + domain.to_glib_none().0, + msgid.to_glib_none().0, + msgid_plural.to_glib_none().0, + n, + )) } } @@ -308,13 +409,21 @@ pub fn dngettext(domain: Option<&str>, msgid: &str, msgid_plural: &str, n: libc: pub fn dpgettext(domain: Option<&str>, msgctxtid: &str, msgidoffset: usize) -> Option { unsafe { - from_glib_none(glib_sys::g_dpgettext(domain.to_glib_none().0, msgctxtid.to_glib_none().0, msgidoffset)) + from_glib_none(glib_sys::g_dpgettext( + domain.to_glib_none().0, + msgctxtid.to_glib_none().0, + msgidoffset, + )) } } pub fn dpgettext2(domain: Option<&str>, context: &str, msgid: &str) -> Option { unsafe { - from_glib_none(glib_sys::g_dpgettext2(domain.to_glib_none().0, context.to_glib_none().0, msgid.to_glib_none().0)) + from_glib_none(glib_sys::g_dpgettext2( + domain.to_glib_none().0, + context.to_glib_none().0, + msgid.to_glib_none().0, + )) } } @@ -327,17 +436,36 @@ pub fn file_get_contents>(filename: P) -> Result>(tmpl: P) -> Result<(i32, std::path::PathBuf), Error> { + let _ = glib_sys::g_file_get_contents( + filename.as_ref().to_glib_none().0, + &mut contents, + &mut length, + &mut error, + ); + if error.is_null() { + Ok(FromGlibContainer::from_glib_full_num( + contents, + length as usize, + )) + } else { + Err(from_glib_full(error)) + } + } +} + +pub fn file_open_tmp>( + tmpl: P, +) -> Result<(i32, std::path::PathBuf), Error> { unsafe { let mut name_used = ptr::null_mut(); let mut error = ptr::null_mut(); - let ret = glib_sys::g_file_open_tmp(tmpl.as_ref().to_glib_none().0, &mut name_used, &mut error); - if error.is_null() { Ok((ret, from_glib_full(name_used))) } else { Err(from_glib_full(error)) } + let ret = + glib_sys::g_file_open_tmp(tmpl.as_ref().to_glib_none().0, &mut name_used, &mut error); + if error.is_null() { + Ok((ret, from_glib_full(name_used))) + } else { + Err(from_glib_full(error)) + } } } @@ -345,47 +473,66 @@ pub fn file_read_link>(filename: P) -> Result>(filename: P, contents: &[u8]) -> Result<(), Error> { +pub fn file_set_contents>( + filename: P, + contents: &[u8], +) -> Result<(), Error> { let length = contents.len() as isize; unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_file_set_contents(filename.as_ref().to_glib_none().0, contents.to_glib_none().0, length, &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + let _ = glib_sys::g_file_set_contents( + filename.as_ref().to_glib_none().0, + contents.to_glib_none().0, + length, + &mut error, + ); + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } pub fn file_test>(filename: P, test: FileTest) -> bool { unsafe { - from_glib(glib_sys::g_file_test(filename.as_ref().to_glib_none().0, test.to_glib())) + from_glib(glib_sys::g_file_test( + filename.as_ref().to_glib_none().0, + test.to_glib(), + )) } } pub fn filename_display_basename>(filename: P) -> Option { unsafe { - from_glib_full(glib_sys::g_filename_display_basename(filename.as_ref().to_glib_none().0)) + from_glib_full(glib_sys::g_filename_display_basename( + filename.as_ref().to_glib_none().0, + )) } } pub fn filename_display_name>(filename: P) -> Option { unsafe { - from_glib_full(glib_sys::g_filename_display_name(filename.as_ref().to_glib_none().0)) + from_glib_full(glib_sys::g_filename_display_name( + filename.as_ref().to_glib_none().0, + )) } } pub fn format_size(size: u64) -> Option { - unsafe { - from_glib_full(glib_sys::g_format_size(size)) - } + unsafe { from_glib_full(glib_sys::g_format_size(size)) } } pub fn format_size_full(size: u64, flags: FormatSizeFlags) -> Option { - unsafe { - from_glib_full(glib_sys::g_format_size_full(size, flags.to_glib())) - } + unsafe { from_glib_full(glib_sys::g_format_size_full(size, flags.to_glib())) } } //pub fn fprintf(file: /*Unimplemented*/Fundamental: Pointer, format: &str, : /*Unknown conversion*//*Unimplemented*/Fundamental: VarArgs) -> i32 { @@ -397,23 +544,23 @@ pub fn format_size_full(size: u64, flags: FormatSizeFlags) -> Option { //} pub fn get_application_name() -> Option { - unsafe { - from_glib_none(glib_sys::g_get_application_name()) - } + unsafe { from_glib_none(glib_sys::g_get_application_name()) } } pub fn get_charset() -> Option { unsafe { let mut charset = ptr::null(); let ret = from_glib(glib_sys::g_get_charset(&mut charset)); - if ret { Some(from_glib_none(charset)) } else { None } + if ret { + Some(from_glib_none(charset)) + } else { + None + } } } pub fn get_codeset() -> Option { - unsafe { - from_glib_full(glib_sys::g_get_codeset()) - } + unsafe { from_glib_full(glib_sys::g_get_codeset()) } } //pub fn get_current_time(result: /*Ignored*/&mut TimeVal) { @@ -421,124 +568,100 @@ pub fn get_codeset() -> Option { //} pub fn get_environ() -> Vec { - unsafe { - FromGlibPtrContainer::from_glib_full(glib_sys::g_get_environ()) - } + unsafe { FromGlibPtrContainer::from_glib_full(glib_sys::g_get_environ()) } } pub fn get_host_name() -> Option { - unsafe { - from_glib_none(glib_sys::g_get_host_name()) - } + unsafe { from_glib_none(glib_sys::g_get_host_name()) } } pub fn get_language_names() -> Vec { - unsafe { - FromGlibPtrContainer::from_glib_none(glib_sys::g_get_language_names()) - } + unsafe { FromGlibPtrContainer::from_glib_none(glib_sys::g_get_language_names()) } } #[cfg(any(feature = "v2_58", feature = "dox"))] pub fn get_language_names_with_category(category_name: &str) -> Vec { unsafe { - FromGlibPtrContainer::from_glib_none(glib_sys::g_get_language_names_with_category(category_name.to_glib_none().0)) + FromGlibPtrContainer::from_glib_none(glib_sys::g_get_language_names_with_category( + category_name.to_glib_none().0, + )) } } pub fn get_locale_variants(locale: &str) -> Vec { unsafe { - FromGlibPtrContainer::from_glib_full(glib_sys::g_get_locale_variants(locale.to_glib_none().0)) + FromGlibPtrContainer::from_glib_full(glib_sys::g_get_locale_variants( + locale.to_glib_none().0, + )) } } pub fn get_monotonic_time() -> i64 { - unsafe { - glib_sys::g_get_monotonic_time() - } + unsafe { glib_sys::g_get_monotonic_time() } } pub fn get_num_processors() -> u32 { - unsafe { - glib_sys::g_get_num_processors() - } + unsafe { glib_sys::g_get_num_processors() } } pub fn get_real_time() -> i64 { - unsafe { - glib_sys::g_get_real_time() - } + unsafe { glib_sys::g_get_real_time() } } pub fn get_system_config_dirs() -> Vec { - unsafe { - FromGlibPtrContainer::from_glib_none(glib_sys::g_get_system_config_dirs()) - } + unsafe { FromGlibPtrContainer::from_glib_none(glib_sys::g_get_system_config_dirs()) } } pub fn get_system_data_dirs() -> Vec { - unsafe { - FromGlibPtrContainer::from_glib_none(glib_sys::g_get_system_data_dirs()) - } + unsafe { FromGlibPtrContainer::from_glib_none(glib_sys::g_get_system_data_dirs()) } } pub fn get_user_cache_dir() -> Option { - unsafe { - from_glib_none(glib_sys::g_get_user_cache_dir()) - } + unsafe { from_glib_none(glib_sys::g_get_user_cache_dir()) } } pub fn get_user_config_dir() -> Option { - unsafe { - from_glib_none(glib_sys::g_get_user_config_dir()) - } + unsafe { from_glib_none(glib_sys::g_get_user_config_dir()) } } pub fn get_user_data_dir() -> Option { - unsafe { - from_glib_none(glib_sys::g_get_user_data_dir()) - } + unsafe { from_glib_none(glib_sys::g_get_user_data_dir()) } } pub fn get_user_runtime_dir() -> Option { - unsafe { - from_glib_none(glib_sys::g_get_user_runtime_dir()) - } + unsafe { from_glib_none(glib_sys::g_get_user_runtime_dir()) } } pub fn get_user_special_dir(directory: UserDirectory) -> Option { - unsafe { - from_glib_none(glib_sys::g_get_user_special_dir(directory.to_glib())) - } + unsafe { from_glib_none(glib_sys::g_get_user_special_dir(directory.to_glib())) } } pub fn hostname_is_ascii_encoded(hostname: &str) -> bool { unsafe { - from_glib(glib_sys::g_hostname_is_ascii_encoded(hostname.to_glib_none().0)) + from_glib(glib_sys::g_hostname_is_ascii_encoded( + hostname.to_glib_none().0, + )) } } pub fn hostname_is_ip_address(hostname: &str) -> bool { unsafe { - from_glib(glib_sys::g_hostname_is_ip_address(hostname.to_glib_none().0)) + from_glib(glib_sys::g_hostname_is_ip_address( + hostname.to_glib_none().0, + )) } } pub fn hostname_is_non_ascii(hostname: &str) -> bool { - unsafe { - from_glib(glib_sys::g_hostname_is_non_ascii(hostname.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_hostname_is_non_ascii(hostname.to_glib_none().0)) } } pub fn hostname_to_ascii(hostname: &str) -> Option { - unsafe { - from_glib_full(glib_sys::g_hostname_to_ascii(hostname.to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_hostname_to_ascii(hostname.to_glib_none().0)) } } pub fn hostname_to_unicode(hostname: &str) -> Option { - unsafe { - from_glib_full(glib_sys::g_hostname_to_unicode(hostname.to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_hostname_to_unicode(hostname.to_glib_none().0)) } } //pub fn iconv(converter: /*Ignored*/&IConv, inbuf: &str, inbytes_left: usize, outbuf: &str, outbytes_left: usize) -> usize { @@ -578,9 +701,7 @@ pub fn hostname_to_unicode(hostname: &str) -> Option { //} pub fn listenv() -> Vec { - unsafe { - FromGlibPtrContainer::from_glib_full(glib_sys::g_listenv()) - } + unsafe { FromGlibPtrContainer::from_glib_full(glib_sys::g_listenv()) } } //pub fn log(log_domain: Option<&str>, log_level: /*Ignored*/LogLevelFlags, format: &str, : /*Unknown conversion*//*Unimplemented*/Fundamental: VarArgs) { @@ -667,15 +788,11 @@ pub fn log_remove_handler(log_domain: &str, handler_id: u32) { //} pub fn main_current_source() -> Option { - unsafe { - from_glib_none(glib_sys::g_main_current_source()) - } + unsafe { from_glib_none(glib_sys::g_main_current_source()) } } pub fn main_depth() -> i32 { - unsafe { - glib_sys::g_main_depth() - } + unsafe { glib_sys::g_main_depth() } } //pub fn malloc(n_bytes: usize) -> /*Unimplemented*/Option { @@ -701,7 +818,10 @@ pub fn main_depth() -> i32 { pub fn markup_escape_text(text: &str) -> GString { let length = text.len() as isize; unsafe { - from_glib_full(glib_sys::g_markup_escape_text(text.to_glib_none().0, length)) + from_glib_full(glib_sys::g_markup_escape_text( + text.to_glib_none().0, + length, + )) } } @@ -715,9 +835,7 @@ pub fn markup_escape_text(text: &str) -> GString { #[cfg_attr(feature = "v2_46", deprecated)] pub fn mem_is_system_malloc() -> bool { - unsafe { - from_glib(glib_sys::g_mem_is_system_malloc()) - } + unsafe { from_glib(glib_sys::g_mem_is_system_malloc()) } } #[cfg_attr(feature = "v2_46", deprecated)] @@ -737,27 +855,24 @@ pub fn mem_profile() { //} pub fn mkdir_with_parents>(pathname: P, mode: i32) -> i32 { - unsafe { - glib_sys::g_mkdir_with_parents(pathname.as_ref().to_glib_none().0, mode) - } + unsafe { glib_sys::g_mkdir_with_parents(pathname.as_ref().to_glib_none().0, mode) } } pub fn mkdtemp>(tmpl: P) -> Option { - unsafe { - from_glib_full(glib_sys::g_mkdtemp(tmpl.as_ref().to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_mkdtemp(tmpl.as_ref().to_glib_none().0)) } } pub fn mkdtemp_full>(tmpl: P, mode: i32) -> Option { unsafe { - from_glib_full(glib_sys::g_mkdtemp_full(tmpl.as_ref().to_glib_none().0, mode)) + from_glib_full(glib_sys::g_mkdtemp_full( + tmpl.as_ref().to_glib_none().0, + mode, + )) } } pub fn mkstemp_full>(tmpl: P, flags: i32, mode: i32) -> i32 { - unsafe { - glib_sys::g_mkstemp_full(tmpl.as_ref().to_glib_none().0, flags, mode) - } + unsafe { glib_sys::g_mkstemp_full(tmpl.as_ref().to_glib_none().0, flags, mode) } } //pub fn nullify_pointer(nullify_location: /*Unimplemented*/Fundamental: Pointer) { @@ -782,25 +897,33 @@ pub fn on_error_stack_trace(prg_name: &str) { pub fn path_get_basename>(file_name: P) -> Option { unsafe { - from_glib_full(glib_sys::g_path_get_basename(file_name.as_ref().to_glib_none().0)) + from_glib_full(glib_sys::g_path_get_basename( + file_name.as_ref().to_glib_none().0, + )) } } pub fn path_get_dirname>(file_name: P) -> Option { unsafe { - from_glib_full(glib_sys::g_path_get_dirname(file_name.as_ref().to_glib_none().0)) + from_glib_full(glib_sys::g_path_get_dirname( + file_name.as_ref().to_glib_none().0, + )) } } pub fn path_is_absolute>(file_name: P) -> bool { unsafe { - from_glib(glib_sys::g_path_is_absolute(file_name.as_ref().to_glib_none().0)) + from_glib(glib_sys::g_path_is_absolute( + file_name.as_ref().to_glib_none().0, + )) } } pub fn path_skip_root>(file_name: P) -> Option { unsafe { - from_glib_none(glib_sys::g_path_skip_root(file_name.as_ref().to_glib_none().0)) + from_glib_none(glib_sys::g_path_skip_root( + file_name.as_ref().to_glib_none().0, + )) } } @@ -810,7 +933,10 @@ pub fn path_skip_root>(file_name: P) -> Option bool { unsafe { - from_glib(glib_sys::g_pattern_match_simple(pattern.to_glib_none().0, string.to_glib_none().0)) + from_glib(glib_sys::g_pattern_match_simple( + pattern.to_glib_none().0, + string.to_glib_none().0, + )) } } @@ -863,27 +989,19 @@ pub fn pattern_match_simple(pattern: &str, string: &str) -> bool { //} pub fn random_double() -> f64 { - unsafe { - glib_sys::g_random_double() - } + unsafe { glib_sys::g_random_double() } } pub fn random_double_range(begin: f64, end: f64) -> f64 { - unsafe { - glib_sys::g_random_double_range(begin, end) - } + unsafe { glib_sys::g_random_double_range(begin, end) } } pub fn random_int() -> u32 { - unsafe { - glib_sys::g_random_int() - } + unsafe { glib_sys::g_random_int() } } pub fn random_int_range(begin: i32, end: i32) -> i32 { - unsafe { - glib_sys::g_random_int_range(begin, end) - } + unsafe { glib_sys::g_random_int_range(begin, end) } } pub fn random_set_seed(seed: u32) { @@ -906,16 +1024,22 @@ pub fn reload_user_special_dirs_cache() { } } -pub fn return_if_fail_warning(log_domain: Option<&str>, pretty_function: &str, expression: Option<&str>) { +pub fn return_if_fail_warning( + log_domain: Option<&str>, + pretty_function: &str, + expression: Option<&str>, +) { unsafe { - glib_sys::g_return_if_fail_warning(log_domain.to_glib_none().0, pretty_function.to_glib_none().0, expression.to_glib_none().0); + glib_sys::g_return_if_fail_warning( + log_domain.to_glib_none().0, + pretty_function.to_glib_none().0, + expression.to_glib_none().0, + ); } } pub fn rmdir>(filename: P) -> i32 { - unsafe { - glib_sys::g_rmdir(filename.as_ref().to_glib_none().0) - } + unsafe { glib_sys::g_rmdir(filename.as_ref().to_glib_none().0) } } pub fn set_application_name(application_name: &str) { @@ -936,27 +1060,46 @@ pub fn set_application_name(application_name: &str) { // unsafe { TODO: call glib_sys:g_set_printerr_handler() } //} -pub fn shell_parse_argv>(command_line: P) -> Result, Error> { +pub fn shell_parse_argv>( + command_line: P, +) -> Result, Error> { unsafe { let mut argcp = mem::uninitialized(); let mut argvp = ptr::null_mut(); let mut error = ptr::null_mut(); - let _ = glib_sys::g_shell_parse_argv(command_line.as_ref().to_glib_none().0, &mut argcp, &mut argvp, &mut error); - if error.is_null() { Ok(FromGlibContainer::from_glib_full_num(argvp, argcp as usize)) } else { Err(from_glib_full(error)) } + let _ = glib_sys::g_shell_parse_argv( + command_line.as_ref().to_glib_none().0, + &mut argcp, + &mut argvp, + &mut error, + ); + if error.is_null() { + Ok(FromGlibContainer::from_glib_full_num(argvp, argcp as usize)) + } else { + Err(from_glib_full(error)) + } } } pub fn shell_quote>(unquoted_string: P) -> Option { unsafe { - from_glib_full(glib_sys::g_shell_quote(unquoted_string.as_ref().to_glib_none().0)) + from_glib_full(glib_sys::g_shell_quote( + unquoted_string.as_ref().to_glib_none().0, + )) } } -pub fn shell_unquote>(quoted_string: P) -> Result { +pub fn shell_unquote>( + quoted_string: P, +) -> Result { unsafe { let mut error = ptr::null_mut(); let ret = glib_sys::g_shell_unquote(quoted_string.as_ref().to_glib_none().0, &mut error); - if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) } + if error.is_null() { + Ok(from_glib_full(ret)) + } else { + Err(from_glib_full(error)) + } } } @@ -997,9 +1140,7 @@ pub fn shell_unquote>(quoted_string: P) -> Result u32 { - unsafe { - glib_sys::g_spaced_primes_closest(num) - } + unsafe { glib_sys::g_spaced_primes_closest(num) } } //pub fn spawn_async>(working_directory: P, argv: &[&std::path::Path], envp: &[&std::path::Path], flags: SpawnFlags, child_setup: Option>) -> Result { @@ -1019,7 +1160,11 @@ pub fn spawn_check_exit_status(exit_status: i32) -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); let _ = glib_sys::g_spawn_check_exit_status(exit_status, &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } @@ -1027,8 +1172,15 @@ pub fn spawn_check_exit_status(exit_status: i32) -> Result<(), Error> { pub fn spawn_command_line_async>(command_line: P) -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_spawn_command_line_async(command_line.as_ref().to_glib_none().0, &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + let _ = glib_sys::g_spawn_command_line_async( + command_line.as_ref().to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } @@ -1046,7 +1198,10 @@ pub fn spawn_command_line_async>(command_line: P) -> R pub fn stpcpy(dest: &str, src: &str) -> Option { unsafe { - from_glib_full(glib_sys::g_stpcpy(dest.to_glib_none().0, src.to_glib_none().0)) + from_glib_full(glib_sys::g_stpcpy( + dest.to_glib_none().0, + src.to_glib_none().0, + )) } } @@ -1083,38 +1238,58 @@ pub fn stpcpy(dest: &str, src: &str) -> Option { //} pub fn unlink>(filename: P) -> i32 { - unsafe { - glib_sys::g_unlink(filename.as_ref().to_glib_none().0) - } + unsafe { glib_sys::g_unlink(filename.as_ref().to_glib_none().0) } } -pub fn uri_escape_string(unescaped: &str, reserved_chars_allowed: Option<&str>, allow_utf8: bool) -> Option { +pub fn uri_escape_string( + unescaped: &str, + reserved_chars_allowed: Option<&str>, + allow_utf8: bool, +) -> Option { unsafe { - from_glib_full(glib_sys::g_uri_escape_string(unescaped.to_glib_none().0, reserved_chars_allowed.to_glib_none().0, allow_utf8.to_glib())) + from_glib_full(glib_sys::g_uri_escape_string( + unescaped.to_glib_none().0, + reserved_chars_allowed.to_glib_none().0, + allow_utf8.to_glib(), + )) } } pub fn uri_list_extract_uris(uri_list: &str) -> Vec { unsafe { - FromGlibPtrContainer::from_glib_full(glib_sys::g_uri_list_extract_uris(uri_list.to_glib_none().0)) + FromGlibPtrContainer::from_glib_full(glib_sys::g_uri_list_extract_uris( + uri_list.to_glib_none().0, + )) } } pub fn uri_parse_scheme(uri: &str) -> Option { - unsafe { - from_glib_full(glib_sys::g_uri_parse_scheme(uri.to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_uri_parse_scheme(uri.to_glib_none().0)) } } -pub fn uri_unescape_segment(escaped_string: Option<&str>, escaped_string_end: Option<&str>, illegal_characters: Option<&str>) -> Option { +pub fn uri_unescape_segment( + escaped_string: Option<&str>, + escaped_string_end: Option<&str>, + illegal_characters: Option<&str>, +) -> Option { unsafe { - from_glib_full(glib_sys::g_uri_unescape_segment(escaped_string.to_glib_none().0, escaped_string_end.to_glib_none().0, illegal_characters.to_glib_none().0)) + from_glib_full(glib_sys::g_uri_unescape_segment( + escaped_string.to_glib_none().0, + escaped_string_end.to_glib_none().0, + illegal_characters.to_glib_none().0, + )) } } -pub fn uri_unescape_string(escaped_string: &str, illegal_characters: Option<&str>) -> Option { +pub fn uri_unescape_string( + escaped_string: &str, + illegal_characters: Option<&str>, +) -> Option { unsafe { - from_glib_full(glib_sys::g_uri_unescape_string(escaped_string.to_glib_none().0, illegal_characters.to_glib_none().0)) + from_glib_full(glib_sys::g_uri_unescape_string( + escaped_string.to_glib_none().0, + illegal_characters.to_glib_none().0, + )) } } @@ -1126,22 +1301,16 @@ pub fn usleep(microseconds: libc::c_ulong) { #[cfg(any(feature = "v2_52", feature = "dox"))] pub fn uuid_string_is_valid(str: &str) -> bool { - unsafe { - from_glib(glib_sys::g_uuid_string_is_valid(str.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_uuid_string_is_valid(str.to_glib_none().0)) } } #[cfg(any(feature = "v2_52", feature = "dox"))] pub fn uuid_string_random() -> Option { - unsafe { - from_glib_full(glib_sys::g_uuid_string_random()) - } + unsafe { from_glib_full(glib_sys::g_uuid_string_random()) } } pub fn variant_get_gtype() -> types::Type { - unsafe { - from_glib(glib_sys::g_variant_get_gtype()) - } + unsafe { from_glib(glib_sys::g_variant_get_gtype()) } } //pub fn vasprintf(string: &str, format: &str, args: /*Unknown conversion*//*Unimplemented*/Unsupported) -> i32 { @@ -1164,8 +1333,20 @@ pub fn variant_get_gtype() -> types::Type { // unsafe { TODO: call glib_sys:g_vsprintf() } //} -pub fn warn_message(domain: Option<&str>, file: &str, line: i32, func: &str, warnexpr: Option<&str>) { - unsafe { - glib_sys::g_warn_message(domain.to_glib_none().0, file.to_glib_none().0, line, func.to_glib_none().0, warnexpr.to_glib_none().0); +pub fn warn_message( + domain: Option<&str>, + file: &str, + line: i32, + func: &str, + warnexpr: Option<&str>, +) { + unsafe { + glib_sys::g_warn_message( + domain.to_glib_none().0, + file.to_glib_none().0, + line, + func.to_glib_none().0, + warnexpr.to_glib_none().0, + ); } } diff --git a/src/auto/key_file.rs b/src/auto/key_file.rs index 942856f6..495e0417 100644 --- a/src/auto/key_file.rs +++ b/src/auto/key_file.rs @@ -2,17 +2,17 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT +use glib_sys; +use std; +use std::mem; +use std::ptr; +use translate::*; #[cfg(any(feature = "v2_50", feature = "dox"))] use Bytes; use Char; use Error; use GString; use KeyFileFlags; -use glib_sys; -use std; -use std::mem; -use std::ptr; -use translate::*; glib_wrapper! { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -27,24 +27,40 @@ glib_wrapper! { impl KeyFile { pub fn new() -> KeyFile { - unsafe { - from_glib_full(glib_sys::g_key_file_new()) - } + unsafe { from_glib_full(glib_sys::g_key_file_new()) } } pub fn get_comment(&self, group_name: Option<&str>, key: &str) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_comment(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_comment( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(from_glib_full(ret)) + } else { + Err(from_glib_full(error)) + } } } pub fn get_double(&self, group_name: &str, key: &str) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_double(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(ret) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_double( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(ret) + } else { + Err(from_glib_full(error)) + } } } @@ -52,15 +68,31 @@ impl KeyFile { unsafe { let mut length = mem::uninitialized(); let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_double_list(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut length, &mut error); - if error.is_null() { Ok(FromGlibContainer::from_glib_container_num(ret, length as usize)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_double_list( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut length, + &mut error, + ); + if error.is_null() { + Ok(FromGlibContainer::from_glib_container_num( + ret, + length as usize, + )) + } else { + Err(from_glib_full(error)) + } } } pub fn get_groups(&self) -> (Vec, usize) { unsafe { let mut length = mem::uninitialized(); - let ret = FromGlibPtrContainer::from_glib_full(glib_sys::g_key_file_get_groups(self.to_glib_none().0, &mut length)); + let ret = FromGlibPtrContainer::from_glib_full(glib_sys::g_key_file_get_groups( + self.to_glib_none().0, + &mut length, + )); (ret, length) } } @@ -68,16 +100,34 @@ impl KeyFile { pub fn get_int64(&self, group_name: &str, key: &str) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_int64(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(ret) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_int64( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(ret) + } else { + Err(from_glib_full(error)) + } } } pub fn get_integer(&self, group_name: &str, key: &str) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_integer(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(ret) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_integer( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(ret) + } else { + Err(from_glib_full(error)) + } } } @@ -85,8 +135,21 @@ impl KeyFile { unsafe { let mut length = mem::uninitialized(); let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_integer_list(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut length, &mut error); - if error.is_null() { Ok(FromGlibContainer::from_glib_container_num(ret, length as usize)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_integer_list( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut length, + &mut error, + ); + if error.is_null() { + Ok(FromGlibContainer::from_glib_container_num( + ret, + length as usize, + )) + } else { + Err(from_glib_full(error)) + } } } @@ -94,46 +157,103 @@ impl KeyFile { unsafe { let mut length = mem::uninitialized(); let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_keys(self.to_glib_none().0, group_name.to_glib_none().0, &mut length, &mut error); - if error.is_null() { Ok((FromGlibPtrContainer::from_glib_full(ret), length)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_keys( + self.to_glib_none().0, + group_name.to_glib_none().0, + &mut length, + &mut error, + ); + if error.is_null() { + Ok((FromGlibPtrContainer::from_glib_full(ret), length)) + } else { + Err(from_glib_full(error)) + } } } #[cfg(any(feature = "v2_56", feature = "dox"))] - pub fn get_locale_for_key(&self, group_name: &str, key: &str, locale: Option<&str>) -> Option { + pub fn get_locale_for_key( + &self, + group_name: &str, + key: &str, + locale: Option<&str>, + ) -> Option { unsafe { - from_glib_full(glib_sys::g_key_file_get_locale_for_key(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, locale.to_glib_none().0)) + from_glib_full(glib_sys::g_key_file_get_locale_for_key( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + locale.to_glib_none().0, + )) } } - pub fn get_locale_string(&self, group_name: &str, key: &str, locale: Option<&str>) -> Result { + pub fn get_locale_string( + &self, + group_name: &str, + key: &str, + locale: Option<&str>, + ) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_locale_string(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, locale.to_glib_none().0, &mut error); - if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) } - } - } - - pub fn get_locale_string_list(&self, group_name: &str, key: &str, locale: Option<&str>) -> Result, Error> { + let ret = glib_sys::g_key_file_get_locale_string( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + locale.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(from_glib_full(ret)) + } else { + Err(from_glib_full(error)) + } + } + } + + pub fn get_locale_string_list( + &self, + group_name: &str, + key: &str, + locale: Option<&str>, + ) -> Result, Error> { unsafe { let mut length = mem::uninitialized(); let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_locale_string_list(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, locale.to_glib_none().0, &mut length, &mut error); - if error.is_null() { Ok(FromGlibContainer::from_glib_full_num(ret, length as usize)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_locale_string_list( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + locale.to_glib_none().0, + &mut length, + &mut error, + ); + if error.is_null() { + Ok(FromGlibContainer::from_glib_full_num(ret, length as usize)) + } else { + Err(from_glib_full(error)) + } } } pub fn get_start_group(&self) -> Option { - unsafe { - from_glib_full(glib_sys::g_key_file_get_start_group(self.to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_key_file_get_start_group(self.to_glib_none().0)) } } pub fn get_string(&self, group_name: &str, key: &str) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_string(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_string( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(from_glib_full(ret)) + } else { + Err(from_glib_full(error)) + } } } @@ -141,30 +261,61 @@ impl KeyFile { unsafe { let mut length = mem::uninitialized(); let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_string_list(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut length, &mut error); - if error.is_null() { Ok(FromGlibContainer::from_glib_full_num(ret, length as usize)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_string_list( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut length, + &mut error, + ); + if error.is_null() { + Ok(FromGlibContainer::from_glib_full_num(ret, length as usize)) + } else { + Err(from_glib_full(error)) + } } } pub fn get_uint64(&self, group_name: &str, key: &str) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_uint64(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(ret) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_uint64( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(ret) + } else { + Err(from_glib_full(error)) + } } } pub fn get_value(&self, group_name: &str, key: &str) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_value(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_value( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(from_glib_full(ret)) + } else { + Err(from_glib_full(error)) + } } } pub fn has_group(&self, group_name: &str) -> bool { unsafe { - from_glib(glib_sys::g_key_file_has_group(self.to_glib_none().0, group_name.to_glib_none().0)) + from_glib(glib_sys::g_key_file_has_group( + self.to_glib_none().0, + group_name.to_glib_none().0, + )) } } @@ -172,8 +323,17 @@ impl KeyFile { pub fn load_from_bytes(&self, bytes: &Bytes, flags: KeyFileFlags) -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_key_file_load_from_bytes(self.to_glib_none().0, bytes.to_glib_none().0, flags.to_glib(), &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + let _ = glib_sys::g_key_file_load_from_bytes( + self.to_glib_none().0, + bytes.to_glib_none().0, + flags.to_glib(), + &mut error, + ); + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } @@ -181,46 +341,100 @@ impl KeyFile { let length = data.len() as usize; unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_key_file_load_from_data(self.to_glib_none().0, data.to_glib_none().0, length, flags.to_glib(), &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } - } - } - - pub fn load_from_file>(&self, file: P, flags: KeyFileFlags) -> Result<(), Error> { + let _ = glib_sys::g_key_file_load_from_data( + self.to_glib_none().0, + data.to_glib_none().0, + length, + flags.to_glib(), + &mut error, + ); + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } + } + } + + pub fn load_from_file>( + &self, + file: P, + flags: KeyFileFlags, + ) -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_key_file_load_from_file(self.to_glib_none().0, file.as_ref().to_glib_none().0, flags.to_glib(), &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + let _ = glib_sys::g_key_file_load_from_file( + self.to_glib_none().0, + file.as_ref().to_glib_none().0, + flags.to_glib(), + &mut error, + ); + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } pub fn remove_comment(&self, group_name: Option<&str>, key: Option<&str>) -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_key_file_remove_comment(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + let _ = glib_sys::g_key_file_remove_comment( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } pub fn remove_group(&self, group_name: &str) -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_key_file_remove_group(self.to_glib_none().0, group_name.to_glib_none().0, &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + let _ = glib_sys::g_key_file_remove_group( + self.to_glib_none().0, + group_name.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } pub fn remove_key(&self, group_name: &str, key: &str) -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_key_file_remove_key(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + let _ = glib_sys::g_key_file_remove_key( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } pub fn set_boolean(&self, group_name: &str, key: &str, value: bool) { unsafe { - glib_sys::g_key_file_set_boolean(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, value.to_glib()); + glib_sys::g_key_file_set_boolean( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + value.to_glib(), + ); } } @@ -228,29 +442,59 @@ impl KeyFile { // unsafe { TODO: call glib_sys:g_key_file_set_boolean_list() } //} - pub fn set_comment(&self, group_name: Option<&str>, key: Option<&str>, comment: &str) -> Result<(), Error> { + pub fn set_comment( + &self, + group_name: Option<&str>, + key: Option<&str>, + comment: &str, + ) -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_key_file_set_comment(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, comment.to_glib_none().0, &mut error); - if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } + let _ = glib_sys::g_key_file_set_comment( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + comment.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(()) + } else { + Err(from_glib_full(error)) + } } } pub fn set_double(&self, group_name: &str, key: &str, value: f64) { unsafe { - glib_sys::g_key_file_set_double(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, value); + glib_sys::g_key_file_set_double( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + value, + ); } } pub fn set_int64(&self, group_name: &str, key: &str, value: i64) { unsafe { - glib_sys::g_key_file_set_int64(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, value); + glib_sys::g_key_file_set_int64( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + value, + ); } } pub fn set_integer(&self, group_name: &str, key: &str, value: i32) { unsafe { - glib_sys::g_key_file_set_integer(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, value); + glib_sys::g_key_file_set_integer( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + value, + ); } } @@ -262,25 +506,46 @@ impl KeyFile { pub fn set_locale_string(&self, group_name: &str, key: &str, locale: &str, string: &str) { unsafe { - glib_sys::g_key_file_set_locale_string(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, locale.to_glib_none().0, string.to_glib_none().0); + glib_sys::g_key_file_set_locale_string( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + locale.to_glib_none().0, + string.to_glib_none().0, + ); } } pub fn set_string(&self, group_name: &str, key: &str, string: &str) { unsafe { - glib_sys::g_key_file_set_string(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, string.to_glib_none().0); + glib_sys::g_key_file_set_string( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + string.to_glib_none().0, + ); } } pub fn set_uint64(&self, group_name: &str, key: &str, value: u64) { unsafe { - glib_sys::g_key_file_set_uint64(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, value); + glib_sys::g_key_file_set_uint64( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + value, + ); } } pub fn set_value(&self, group_name: &str, key: &str, value: &str) { unsafe { - glib_sys::g_key_file_set_value(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, value.to_glib_none().0); + glib_sys::g_key_file_set_value( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + value.to_glib_none().0, + ); } } } diff --git a/src/auto/main_context.rs b/src/auto/main_context.rs index 0aeb32af..c5ab594e 100644 --- a/src/auto/main_context.rs +++ b/src/auto/main_context.rs @@ -18,15 +18,11 @@ glib_wrapper! { impl MainContext { pub fn new() -> MainContext { - unsafe { - from_glib_full(glib_sys::g_main_context_new()) - } + unsafe { from_glib_full(glib_sys::g_main_context_new()) } } pub fn acquire(&self) -> bool { - unsafe { - from_glib(glib_sys::g_main_context_acquire(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_main_context_acquire(self.to_glib_none().0)) } } //pub fn add_poll(&self, fd: /*Ignored*/&mut PollFD, priority: i32) { @@ -56,21 +52,20 @@ impl MainContext { //} pub fn is_owner(&self) -> bool { - unsafe { - from_glib(glib_sys::g_main_context_is_owner(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_main_context_is_owner(self.to_glib_none().0)) } } pub fn iteration(&self, may_block: bool) -> bool { unsafe { - from_glib(glib_sys::g_main_context_iteration(self.to_glib_none().0, may_block.to_glib())) + from_glib(glib_sys::g_main_context_iteration( + self.to_glib_none().0, + may_block.to_glib(), + )) } } pub fn pending(&self) -> bool { - unsafe { - from_glib(glib_sys::g_main_context_pending(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_main_context_pending(self.to_glib_none().0)) } } pub fn pop_thread_default(&self) { @@ -115,21 +110,15 @@ impl MainContext { } pub fn default() -> MainContext { - unsafe { - from_glib_none(glib_sys::g_main_context_default()) - } + unsafe { from_glib_none(glib_sys::g_main_context_default()) } } pub fn get_thread_default() -> Option { - unsafe { - from_glib_none(glib_sys::g_main_context_get_thread_default()) - } + unsafe { from_glib_none(glib_sys::g_main_context_get_thread_default()) } } pub fn ref_thread_default() -> MainContext { - unsafe { - from_glib_full(glib_sys::g_main_context_ref_thread_default()) - } + unsafe { from_glib_full(glib_sys::g_main_context_ref_thread_default()) } } } diff --git a/src/auto/main_loop.rs b/src/auto/main_loop.rs index fd988f81..bad23bb3 100644 --- a/src/auto/main_loop.rs +++ b/src/auto/main_loop.rs @@ -2,9 +2,9 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT -use MainContext; use glib_sys; use translate::*; +use MainContext; glib_wrapper! { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -20,20 +20,19 @@ glib_wrapper! { impl MainLoop { pub fn new(context: Option<&MainContext>, is_running: bool) -> MainLoop { unsafe { - from_glib_full(glib_sys::g_main_loop_new(context.to_glib_none().0, is_running.to_glib())) + from_glib_full(glib_sys::g_main_loop_new( + context.to_glib_none().0, + is_running.to_glib(), + )) } } pub fn get_context(&self) -> MainContext { - unsafe { - from_glib_none(glib_sys::g_main_loop_get_context(self.to_glib_none().0)) - } + unsafe { from_glib_none(glib_sys::g_main_loop_get_context(self.to_glib_none().0)) } } pub fn is_running(&self) -> bool { - unsafe { - from_glib(glib_sys::g_main_loop_is_running(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_main_loop_is_running(self.to_glib_none().0)) } } pub fn quit(&self) { diff --git a/src/auto/mod.rs b/src/auto/mod.rs index e8bc18dd..6342d624 100644 --- a/src/auto/mod.rs +++ b/src/auto/mod.rs @@ -49,9 +49,9 @@ pub use self::alias::TimeSpan; pub mod functions; mod constants; +pub use self::constants::CSET_a_2_z; pub use self::constants::CSET_A_2_Z; pub use self::constants::CSET_DIGITS; -pub use self::constants::CSET_a_2_z; pub use self::constants::KEY_FILE_DESKTOP_ACTION_GROUP_PREFIX; pub use self::constants::KEY_FILE_DESKTOP_GROUP; pub use self::constants::KEY_FILE_DESKTOP_KEY_ACTIONS; @@ -87,5 +87,4 @@ pub use self::constants::URI_RESERVED_CHARS_GENERIC_DELIMITERS; pub use self::constants::URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS; #[doc(hidden)] -pub mod traits { -} +pub mod traits {} diff --git a/src/auto/source.rs b/src/auto/source.rs index a3edc222..d5af59d3 100644 --- a/src/auto/source.rs +++ b/src/auto/source.rs @@ -2,10 +2,10 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT -use GString; -use MainContext; use glib_sys; use translate::*; +use GString; +use MainContext; glib_wrapper! { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -25,7 +25,10 @@ impl Source { pub fn add_child_source(&self, child_source: &Source) { unsafe { - glib_sys::g_source_add_child_source(self.to_glib_none().0, child_source.to_glib_none().0); + glib_sys::g_source_add_child_source( + self.to_glib_none().0, + child_source.to_glib_none().0, + ); } } @@ -44,45 +47,31 @@ impl Source { } pub fn get_can_recurse(&self) -> bool { - unsafe { - from_glib(glib_sys::g_source_get_can_recurse(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_source_get_can_recurse(self.to_glib_none().0)) } } pub fn get_context(&self) -> Option { - unsafe { - from_glib_none(glib_sys::g_source_get_context(self.to_glib_none().0)) - } + unsafe { from_glib_none(glib_sys::g_source_get_context(self.to_glib_none().0)) } } pub fn get_name(&self) -> Option { - unsafe { - from_glib_none(glib_sys::g_source_get_name(self.to_glib_none().0)) - } + unsafe { from_glib_none(glib_sys::g_source_get_name(self.to_glib_none().0)) } } pub fn get_priority(&self) -> i32 { - unsafe { - glib_sys::g_source_get_priority(self.to_glib_none().0) - } + unsafe { glib_sys::g_source_get_priority(self.to_glib_none().0) } } pub fn get_ready_time(&self) -> i64 { - unsafe { - glib_sys::g_source_get_ready_time(self.to_glib_none().0) - } + unsafe { glib_sys::g_source_get_ready_time(self.to_glib_none().0) } } pub fn get_time(&self) -> i64 { - unsafe { - glib_sys::g_source_get_time(self.to_glib_none().0) - } + unsafe { glib_sys::g_source_get_time(self.to_glib_none().0) } } pub fn is_destroyed(&self) -> bool { - unsafe { - from_glib(glib_sys::g_source_is_destroyed(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_source_is_destroyed(self.to_glib_none().0)) } } //pub fn modify_unix_fd(&self, tag: /*Unimplemented*/Fundamental: Pointer, new_events: IOCondition) { @@ -95,7 +84,10 @@ impl Source { pub fn remove_child_source(&self, child_source: &Source) { unsafe { - glib_sys::g_source_remove_child_source(self.to_glib_none().0, child_source.to_glib_none().0); + glib_sys::g_source_remove_child_source( + self.to_glib_none().0, + child_source.to_glib_none().0, + ); } } diff --git a/src/auto/time_zone.rs b/src/auto/time_zone.rs index 480e8254..19ed863a 100644 --- a/src/auto/time_zone.rs +++ b/src/auto/time_zone.rs @@ -2,10 +2,10 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT -use GString; -use TimeType; use glib_sys; use translate::*; +use GString; +use TimeType; glib_wrapper! { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -20,28 +20,20 @@ glib_wrapper! { impl TimeZone { pub fn new(identifier: Option<&str>) -> TimeZone { - unsafe { - from_glib_full(glib_sys::g_time_zone_new(identifier.to_glib_none().0)) - } + unsafe { from_glib_full(glib_sys::g_time_zone_new(identifier.to_glib_none().0)) } } pub fn new_local() -> TimeZone { - unsafe { - from_glib_full(glib_sys::g_time_zone_new_local()) - } + unsafe { from_glib_full(glib_sys::g_time_zone_new_local()) } } #[cfg(any(feature = "v2_58", feature = "dox"))] pub fn new_offset(seconds: i32) -> TimeZone { - unsafe { - from_glib_full(glib_sys::g_time_zone_new_offset(seconds)) - } + unsafe { from_glib_full(glib_sys::g_time_zone_new_offset(seconds)) } } pub fn new_utc() -> TimeZone { - unsafe { - from_glib_full(glib_sys::g_time_zone_new_utc()) - } + unsafe { from_glib_full(glib_sys::g_time_zone_new_utc()) } } pub fn find_interval(&self, type_: TimeType, time_: i64) -> i32 { @@ -52,26 +44,28 @@ impl TimeZone { pub fn get_abbreviation(&self, interval: i32) -> Option { unsafe { - from_glib_none(glib_sys::g_time_zone_get_abbreviation(self.to_glib_none().0, interval)) + from_glib_none(glib_sys::g_time_zone_get_abbreviation( + self.to_glib_none().0, + interval, + )) } } #[cfg(any(feature = "v2_58", feature = "dox"))] pub fn get_identifier(&self) -> Option { - unsafe { - from_glib_none(glib_sys::g_time_zone_get_identifier(self.to_glib_none().0)) - } + unsafe { from_glib_none(glib_sys::g_time_zone_get_identifier(self.to_glib_none().0)) } } pub fn get_offset(&self, interval: i32) -> i32 { - unsafe { - glib_sys::g_time_zone_get_offset(self.to_glib_none().0, interval) - } + unsafe { glib_sys::g_time_zone_get_offset(self.to_glib_none().0, interval) } } pub fn is_dst(&self, interval: i32) -> bool { unsafe { - from_glib(glib_sys::g_time_zone_is_dst(self.to_glib_none().0, interval)) + from_glib(glib_sys::g_time_zone_is_dst( + self.to_glib_none().0, + interval, + )) } } } diff --git a/src/boxed.rs b/src/boxed.rs index d6d4a7ad..d3ae4e1b 100644 --- a/src/boxed.rs +++ b/src/boxed.rs @@ -274,15 +274,9 @@ impl fmt::Debug for AnyBox { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::AnyBox::*; match *self { - Native(ref b) => f.debug_tuple("Native") - .field(&(&**b as *const T)) - .finish(), - ForeignOwned(ptr) => f.debug_tuple("ForeignOwned") - .field(&ptr) - .finish(), - ForeignBorrowed(ptr) => f.debug_tuple("ForeignBorrowed") - .field(&ptr) - .finish(), + Native(ref b) => f.debug_tuple("Native").field(&(&**b as *const T)).finish(), + ForeignOwned(ptr) => f.debug_tuple("ForeignOwned").field(&ptr).finish(), + ForeignBorrowed(ptr) => f.debug_tuple("ForeignBorrowed").field(&ptr).finish(), } } } @@ -412,9 +406,7 @@ impl> Drop for Boxed { impl> fmt::Debug for Boxed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Boxed") - .field("inner", &self.inner) - .finish() + f.debug_struct("Boxed").field("inner", &self.inner).finish() } } @@ -439,7 +431,10 @@ impl> PartialEq for Boxed { impl> Eq for Boxed {} impl> Hash for Boxed { - fn hash(&self, state: &mut H) where H: Hasher { + fn hash(&self, state: &mut H) + where + H: Hasher, + { self.to_glib_none().0.hash(state) } } @@ -447,9 +442,7 @@ impl> Hash for Boxed { impl> Clone for Boxed { #[inline] fn clone(&self) -> Self { - unsafe { - from_glib_none(self.to_glib_none().0 as *mut T) - } + unsafe { from_glib_none(self.to_glib_none().0 as *mut T) } } } diff --git a/src/byte_array.rs b/src/byte_array.rs index 91859053..d9e48979 100644 --- a/src/byte_array.rs +++ b/src/byte_array.rs @@ -21,9 +21,9 @@ use std::fmt; use std::hash::{Hash, Hasher}; use std::mem; use std::ops::Deref; +use std::ptr::NonNull; use std::slice; use translate::*; -use std::ptr::NonNull; use Bytes; diff --git a/src/bytes.rs b/src/bytes.rs index 29d56f68..be10aa2a 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -47,7 +47,10 @@ impl Bytes { /// Creates a view into static `data` without copying. pub fn from_static(data: &'static [u8]) -> Bytes { unsafe { - from_glib_full(glib_sys::g_bytes_new_static(data.as_ptr() as *const _, data.len())) + from_glib_full(glib_sys::g_bytes_new_static( + data.as_ptr() as *const _, + data.len(), + )) } } @@ -64,13 +67,18 @@ impl Bytes { } unsafe { - from_glib_full(glib_sys::g_bytes_new_with_free_func(data_ptr as *const _, size, Some(drop_box::), Box::into_raw(data) as *mut _)) + from_glib_full(glib_sys::g_bytes_new_with_free_func( + data_ptr as *const _, + size, + Some(drop_box::), + Box::into_raw(data) as *mut _, + )) } } } -unsafe impl Send for Bytes { } -unsafe impl Sync for Bytes { } +unsafe impl Send for Bytes {} +unsafe impl Sync for Bytes {} impl<'a, T: ?Sized + Borrow<[u8]> + 'a> From<&'a T> for Bytes { fn from(value: &'a T) -> Bytes { @@ -109,20 +117,23 @@ impl Deref for Bytes { impl PartialEq for Bytes { fn eq(&self, other: &Self) -> bool { unsafe { - from_glib( - glib_sys::g_bytes_equal(self.to_glib_none().0 as *const _, - other.to_glib_none().0 as *const _)) + from_glib(glib_sys::g_bytes_equal( + self.to_glib_none().0 as *const _, + other.to_glib_none().0 as *const _, + )) } } } -impl Eq for Bytes { } +impl Eq for Bytes {} impl PartialOrd for Bytes { fn partial_cmp(&self, other: &Self) -> Option { unsafe { - let ret = glib_sys::g_bytes_compare(self.to_glib_none().0 as *const _, - other.to_glib_none().0 as *const _); + let ret = glib_sys::g_bytes_compare( + self.to_glib_none().0 as *const _, + other.to_glib_none().0 as *const _, + ); ret.partial_cmp(&0) } } @@ -131,8 +142,10 @@ impl PartialOrd for Bytes { impl Ord for Bytes { fn cmp(&self, other: &Self) -> Ordering { unsafe { - let ret = glib_sys::g_bytes_compare(self.to_glib_none().0 as *const _, - other.to_glib_none().0 as *const _); + let ret = glib_sys::g_bytes_compare( + self.to_glib_none().0 as *const _, + other.to_glib_none().0 as *const _, + ); ret.cmp(&0) } } @@ -167,7 +180,7 @@ macro_rules! impl_cmp { self[..].partial_cmp(&other[..]) } } - } + }; } impl_cmp!(Bytes, [u8]); @@ -185,8 +198,8 @@ impl Hash for Bytes { #[cfg(test)] mod tests { - use std::collections::HashSet; use super::*; + use std::collections::HashSet; #[test] fn eq() { diff --git a/src/checksum.rs b/src/checksum.rs index 570ecbab..cc88b169 100644 --- a/src/checksum.rs +++ b/src/checksum.rs @@ -28,9 +28,9 @@ impl Checksum { pub fn get_string(self) -> Option { unsafe { - from_glib_none(glib_sys::g_checksum_get_string( - mut_override(self.to_glib_none().0), - )) + from_glib_none(glib_sys::g_checksum_get_string(mut_override( + self.to_glib_none().0, + ))) } } } @@ -42,21 +42,7 @@ mod tests { const CS_TYPE: ChecksumType = ChecksumType::Md5; const CS_VALUE: &str = "fc3ff98e8c6a0d3087d515c0473f8677"; const CS_SLICE: &[u8] = &[ - 0xfc, - 0x3f, - 0xf9, - 0x8e, - 0x8c, - 0x6a, - 0x0d, - 0x30, - 0x87, - 0xd5, - 0x15, - 0xc0, - 0x47, - 0x3f, - 0x86, + 0xfc, 0x3f, 0xf9, 0x8e, 0x8c, 0x6a, 0x0d, 0x30, 0x87, 0xd5, 0x15, 0xc0, 0x47, 0x3f, 0x86, 0x77, ]; diff --git a/src/closure.rs b/src/closure.rs index 57e3da5c..bb272dcb 100644 --- a/src/closure.rs +++ b/src/closure.rs @@ -32,16 +32,19 @@ glib_wrapper! { impl Closure { pub fn new Option + Send + Sync + 'static>(callback: F) -> Self { - unsafe { - Closure::new_unsafe(callback) - } + unsafe { Closure::new_unsafe(callback) } } pub unsafe fn new_unsafe Option>(callback: F) -> Self { - unsafe extern "C" fn marshal(_closure: *mut gobject_sys::GClosure, return_value: *mut gobject_sys::GValue, - n_param_values: c_uint, param_values: *const gobject_sys::GValue, _invocation_hint: *mut c_void, - marshal_data: *mut c_void) - where F: Fn(&[Value]) -> Option + unsafe extern "C" fn marshal( + _closure: *mut gobject_sys::GClosure, + return_value: *mut gobject_sys::GValue, + n_param_values: c_uint, + param_values: *const gobject_sys::GValue, + _invocation_hint: *mut c_void, + marshal_data: *mut c_void, + ) where + F: Fn(&[Value]) -> Option, { let values = slice::from_raw_parts(param_values as *const _, n_param_values as usize); let callback: Box = Box::from_raw(marshal_data as *mut _); @@ -52,14 +55,17 @@ impl Closure { None => { let result = Value::uninitialized(); *return_value = result.into_raw(); - }, + } } } mem::forget(callback); } - unsafe extern "C" fn finalize(notify_data: *mut c_void, _closure: *mut gobject_sys::GClosure) - where F: Fn(&[Value]) -> Option + unsafe extern "C" fn finalize( + notify_data: *mut c_void, + _closure: *mut gobject_sys::GClosure, + ) where + F: Fn(&[Value]) -> Option, { let _callback: Box = Box::from_raw(notify_data as *mut _); // callback is dropped here. @@ -94,15 +100,18 @@ impl Closure { } &s_args[0..values.len()] } else { - v_args = values.iter() - .map(|v| v.to_value()) - .collect(); + v_args = values.iter().map(|v| v.to_value()).collect(); v_args.as_slice() }; unsafe { - gobject_sys::g_closure_invoke(self.to_glib_none().0 as *mut _, result.to_glib_none_mut().0, - values.len() as u32, mut_override(values.as_ptr()) as *mut gobject_sys::GValue, ptr::null_mut()); + gobject_sys::g_closure_invoke( + self.to_glib_none().0 as *mut _, + result.to_glib_none_mut().0, + values.len() as u32, + mut_override(values.as_ptr()) as *mut gobject_sys::GValue, + ptr::null_mut(), + ); } if result.type_() == Type::Invalid { None @@ -117,8 +126,8 @@ unsafe impl Sync for Closure {} #[cfg(test)] mod tests { - use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::Arc; use super::Closure; use ToValue; diff --git a/src/date.rs b/src/date.rs index 8893d2ed..71fbd33b 100644 --- a/src/date.rs +++ b/src/date.rs @@ -25,26 +25,20 @@ glib_wrapper! { } } -unsafe impl Send for Date { } -unsafe impl Sync for Date { } +unsafe impl Send for Date {} +unsafe impl Sync for Date {} impl Date { pub fn new() -> Date { - unsafe { - from_glib_full(glib_sys::g_date_new()) - } + unsafe { from_glib_full(glib_sys::g_date_new()) } } pub fn new_dmy(day: DateDay, month: DateMonth, year: DateYear) -> Date { - unsafe { - from_glib_full(glib_sys::g_date_new_dmy(day, month.to_glib(), year)) - } + unsafe { from_glib_full(glib_sys::g_date_new_dmy(day, month.to_glib(), year)) } } pub fn new_julian(julian_day: u32) -> Date { - unsafe { - from_glib_full(glib_sys::g_date_new_julian(julian_day)) - } + unsafe { from_glib_full(glib_sys::g_date_new_julian(julian_day)) } } pub fn add_days(&mut self, n_days: u32) { @@ -67,7 +61,11 @@ impl Date { pub fn clamp(&mut self, min_date: &Date, max_date: &Date) { unsafe { - glib_sys::g_date_clamp(self.to_glib_none_mut().0, min_date.to_glib_none().0, max_date.to_glib_none().0); + glib_sys::g_date_clamp( + self.to_glib_none_mut().0, + min_date.to_glib_none().0, + max_date.to_glib_none().0, + ); } } @@ -78,81 +76,55 @@ impl Date { } fn compare(&self, rhs: &Date) -> i32 { - unsafe { - glib_sys::g_date_compare(self.to_glib_none().0, rhs.to_glib_none().0) - } + unsafe { glib_sys::g_date_compare(self.to_glib_none().0, rhs.to_glib_none().0) } } pub fn days_between(&self, date2: &Date) -> i32 { - unsafe { - glib_sys::g_date_days_between(self.to_glib_none().0, date2.to_glib_none().0) - } + unsafe { glib_sys::g_date_days_between(self.to_glib_none().0, date2.to_glib_none().0) } } pub fn get_day(&self) -> DateDay { - unsafe { - glib_sys::g_date_get_day(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_get_day(self.to_glib_none().0) } } pub fn get_day_of_year(&self) -> u32 { - unsafe { - glib_sys::g_date_get_day_of_year(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_get_day_of_year(self.to_glib_none().0) } } pub fn get_iso8601_week_of_year(&self) -> u32 { - unsafe { - glib_sys::g_date_get_iso8601_week_of_year(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_get_iso8601_week_of_year(self.to_glib_none().0) } } pub fn get_julian(&self) -> u32 { - unsafe { - glib_sys::g_date_get_julian(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_get_julian(self.to_glib_none().0) } } pub fn get_monday_week_of_year(&self) -> u32 { - unsafe { - glib_sys::g_date_get_monday_week_of_year(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_get_monday_week_of_year(self.to_glib_none().0) } } pub fn get_month(&self) -> DateMonth { - unsafe { - from_glib(glib_sys::g_date_get_month(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_date_get_month(self.to_glib_none().0)) } } pub fn get_sunday_week_of_year(&self) -> u32 { - unsafe { - glib_sys::g_date_get_sunday_week_of_year(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_get_sunday_week_of_year(self.to_glib_none().0) } } pub fn get_weekday(&self) -> DateWeekday { - unsafe { - from_glib(glib_sys::g_date_get_weekday(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_date_get_weekday(self.to_glib_none().0)) } } pub fn get_year(&self) -> DateYear { - unsafe { - glib_sys::g_date_get_year(self.to_glib_none().0) - } + unsafe { glib_sys::g_date_get_year(self.to_glib_none().0) } } pub fn is_first_of_month(&self) -> bool { - unsafe { - from_glib(glib_sys::g_date_is_first_of_month(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_date_is_first_of_month(self.to_glib_none().0)) } } pub fn is_last_of_month(&self) -> bool { - unsafe { - from_glib(glib_sys::g_date_is_last_of_month(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_date_is_last_of_month(self.to_glib_none().0)) } } pub fn order(&mut self, date2: &mut Date) { @@ -236,76 +208,59 @@ impl Date { //} pub fn valid(&self) -> bool { - unsafe { - from_glib(glib_sys::g_date_valid(self.to_glib_none().0)) - } + unsafe { from_glib(glib_sys::g_date_valid(self.to_glib_none().0)) } } pub fn get_days_in_month(month: DateMonth, year: DateYear) -> u8 { - unsafe { - glib_sys::g_date_get_days_in_month(month.to_glib(), year) - } + unsafe { glib_sys::g_date_get_days_in_month(month.to_glib(), year) } } pub fn get_monday_weeks_in_year(year: DateYear) -> u8 { - unsafe { - glib_sys::g_date_get_monday_weeks_in_year(year) - } + unsafe { glib_sys::g_date_get_monday_weeks_in_year(year) } } pub fn get_sunday_weeks_in_year(year: DateYear) -> u8 { - unsafe { - glib_sys::g_date_get_sunday_weeks_in_year(year) - } + unsafe { glib_sys::g_date_get_sunday_weeks_in_year(year) } } pub fn is_leap_year(year: DateYear) -> bool { - unsafe { - from_glib(glib_sys::g_date_is_leap_year(year)) - } + unsafe { from_glib(glib_sys::g_date_is_leap_year(year)) } } pub fn strftime(s: &str, format: &str, date: &Date) -> usize { let slen = s.len() as usize; unsafe { - glib_sys::g_date_strftime(s.to_glib_none().0, slen, format.to_glib_none().0, date.to_glib_none().0) + glib_sys::g_date_strftime( + s.to_glib_none().0, + slen, + format.to_glib_none().0, + date.to_glib_none().0, + ) } } pub fn valid_day(day: DateDay) -> bool { - unsafe { - from_glib(glib_sys::g_date_valid_day(day)) - } + unsafe { from_glib(glib_sys::g_date_valid_day(day)) } } pub fn valid_dmy(day: DateDay, month: DateMonth, year: DateYear) -> bool { - unsafe { - from_glib(glib_sys::g_date_valid_dmy(day, month.to_glib(), year)) - } + unsafe { from_glib(glib_sys::g_date_valid_dmy(day, month.to_glib(), year)) } } pub fn valid_julian(julian_date: u32) -> bool { - unsafe { - from_glib(glib_sys::g_date_valid_julian(julian_date)) - } + unsafe { from_glib(glib_sys::g_date_valid_julian(julian_date)) } } pub fn valid_month(month: DateMonth) -> bool { - unsafe { - from_glib(glib_sys::g_date_valid_month(month.to_glib())) - } + unsafe { from_glib(glib_sys::g_date_valid_month(month.to_glib())) } } pub fn valid_weekday(weekday: DateWeekday) -> bool { - unsafe { - from_glib(glib_sys::g_date_valid_weekday(weekday.to_glib())) - } + unsafe { from_glib(glib_sys::g_date_valid_weekday(weekday.to_glib())) } } pub fn valid_year(year: DateYear) -> bool { - unsafe { - from_glib(glib_sys::g_date_valid_year(year)) - } + unsafe { from_glib(glib_sys::g_date_valid_year(year)) } } } @@ -349,7 +304,10 @@ impl fmt::Debug for Date { } impl hash::Hash for Date { - fn hash(&self, state: &mut H) where H: hash::Hasher { + fn hash(&self, state: &mut H) + where + H: hash::Hasher, + { self.get_year().hash(state); self.get_month().hash(state); self.get_day().hash(state); diff --git a/src/enums.rs b/src/enums.rs index a79c8174..9267998e 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -54,20 +54,23 @@ impl EnumClass { /// Returns `None` if `type_` is not representing an enum. pub fn new(type_: Type) -> Option { unsafe { - let is_enum: bool = from_glib(gobject_sys::g_type_is_a(type_.to_glib(), gobject_sys::G_TYPE_ENUM)); + let is_enum: bool = from_glib(gobject_sys::g_type_is_a( + type_.to_glib(), + gobject_sys::G_TYPE_ENUM, + )); if !is_enum { return None; } - Some(EnumClass(gobject_sys::g_type_class_ref(type_.to_glib()) as *mut _)) + Some(EnumClass( + gobject_sys::g_type_class_ref(type_.to_glib()) as *mut _ + )) } } /// `Type` of the enum. pub fn type_(&self) -> Type { - unsafe { - from_glib((*self.0).g_type_class.g_type) - } + unsafe { from_glib((*self.0).g_type_class.g_type) } } /// Gets `EnumValue` by integer `value`, if existing. @@ -153,9 +156,7 @@ impl Drop for EnumClass { impl Clone for EnumClass { fn clone(&self) -> Self { - unsafe { - EnumClass(gobject_sys::g_type_class_ref(self.type_().to_glib()) as *mut _) - } + unsafe { EnumClass(gobject_sys::g_type_class_ref(self.type_().to_glib()) as *mut _) } } } @@ -166,23 +167,17 @@ pub struct EnumValue(*const gobject_sys::GEnumValue, EnumClass); impl EnumValue { /// Get integer value corresponding to the value. pub fn get_value(&self) -> i32 { - unsafe { - (*self.0).value - } + unsafe { (*self.0).value } } /// Get name corresponding to the value. pub fn get_name(&self) -> &str { - unsafe { - CStr::from_ptr((*self.0).value_name).to_str().unwrap() - } + unsafe { CStr::from_ptr((*self.0).value_name).to_str().unwrap() } } /// Get nick corresponding to the value. pub fn get_nick(&self) -> &str { - unsafe { - CStr::from_ptr((*self.0).value_nick).to_str().unwrap() - } + unsafe { CStr::from_ptr((*self.0).value_nick).to_str().unwrap() } } /// Convert enum value to a `Value`. @@ -198,7 +193,8 @@ impl EnumValue { pub fn from_value(value: &Value) -> Option { unsafe { let enum_class = EnumClass::new(value.type_()); - enum_class.and_then(|e| e.get_value(gobject_sys::g_value_get_enum(value.to_glib_none().0))) + enum_class + .and_then(|e| e.get_value(gobject_sys::g_value_get_enum(value.to_glib_none().0))) } } @@ -239,20 +235,23 @@ impl FlagsClass { /// Returns `None` if `type_` is not representing a flags type. pub fn new(type_: Type) -> Option { unsafe { - let is_flags: bool = from_glib(gobject_sys::g_type_is_a(type_.to_glib(), gobject_sys::G_TYPE_FLAGS)); + let is_flags: bool = from_glib(gobject_sys::g_type_is_a( + type_.to_glib(), + gobject_sys::G_TYPE_FLAGS, + )); if !is_flags { return None; } - Some(FlagsClass(gobject_sys::g_type_class_ref(type_.to_glib()) as *mut _)) + Some(FlagsClass( + gobject_sys::g_type_class_ref(type_.to_glib()) as *mut _ + )) } } /// `Type` of the flags. pub fn type_(&self) -> Type { - unsafe { - from_glib((*self.0).g_type_class.g_type) - } + unsafe { from_glib((*self.0).g_type_class.g_type) } } /// Gets `FlagsValue` by integer `value`, if existing. @@ -524,13 +523,10 @@ impl Drop for FlagsClass { impl Clone for FlagsClass { fn clone(&self) -> Self { - unsafe { - FlagsClass(gobject_sys::g_type_class_ref(self.type_().to_glib()) as *mut _) - } + unsafe { FlagsClass(gobject_sys::g_type_class_ref(self.type_().to_glib()) as *mut _) } } } - /// Representation of a single flags value of a `FlagsClass`. #[derive(Debug, Clone)] pub struct FlagsValue(*const gobject_sys::GFlagsValue, FlagsClass); @@ -538,23 +534,17 @@ pub struct FlagsValue(*const gobject_sys::GFlagsValue, FlagsClass); impl FlagsValue { /// Get integer value corresponding to the value. pub fn get_value(&self) -> u32 { - unsafe { - (*self.0).value - } + unsafe { (*self.0).value } } /// Get name corresponding to the value. pub fn get_name(&self) -> &str { - unsafe { - CStr::from_ptr((*self.0).value_name).to_str().unwrap() - } + unsafe { CStr::from_ptr((*self.0).value_name).to_str().unwrap() } } /// Get nick corresponding to the value. pub fn get_nick(&self) -> &str { - unsafe { - CStr::from_ptr((*self.0).value_nick).to_str().unwrap() - } + unsafe { CStr::from_ptr((*self.0).value_nick).to_str().unwrap() } } /// Convert flags value to a `Value`. diff --git a/src/error.rs b/src/error.rs index c5dab796..1322b7d3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -32,8 +32,11 @@ impl Error { /// Creates an error with supplied error enum variant and message. pub fn new(error: T, message: &str) -> Error { unsafe { - from_glib_full( - glib_sys::g_error_new_literal(T::domain().to_glib(), error.code(), message.to_glib_none().0)) + from_glib_full(glib_sys::g_error_new_literal( + T::domain().to_glib(), + error.code(), + message.to_glib_none().0, + )) } } @@ -69,8 +72,7 @@ impl Error { pub fn kind(&self) -> Option { if self.0.domain == T::domain().to_glib() { T::from(self.0.code) - } - else { + } else { None } } @@ -78,9 +80,8 @@ impl Error { fn message(&self) -> &str { unsafe { let bytes = CStr::from_ptr(self.0.message).to_bytes(); - str::from_utf8(bytes).unwrap_or_else(|err| { - str::from_utf8(&bytes[..err.valid_up_to()]).unwrap() - }) + str::from_utf8(bytes) + .unwrap_or_else(|err| str::from_utf8(&bytes[..err.valid_up_to()]).unwrap()) } } } @@ -123,7 +124,9 @@ pub trait ErrorDomain: Copy { /// /// By convention, the `Failed` variant, if present, is a catch-all, /// i.e. any unrecognized codes map to it. - fn from(code: i32) -> Option where Self: Sized; + fn from(code: i32) -> Option + where + Self: Sized; } /// Generic error used for functions that fail without any further information @@ -193,12 +196,7 @@ impl BoolError { line: u32, ) -> Result<(), Self> { match b { - glib_sys::GFALSE => Err(BoolError::new( - message, - filename, - function, - line, - )), + glib_sys::GFALSE => Err(BoolError::new(message, filename, function, line)), _ => Ok(()), } } @@ -242,20 +240,13 @@ mod tests { let true_static_res = glib_result_from_gboolean!(glib_sys::GTRUE, "Static message"); assert!(true_static_res.is_ok()); - let false_dynamic_res = glib_result_from_gboolean!( - glib_sys::GFALSE, - "{} message", - "Dynamic" - ); + let false_dynamic_res = + glib_result_from_gboolean!(glib_sys::GFALSE, "{} message", "Dynamic"); assert!(false_dynamic_res.is_err()); let dynamic_err = false_dynamic_res.err().unwrap(); assert_eq!(dynamic_err.description(), "Dynamic message"); - let true_dynamic_res = glib_result_from_gboolean!( - glib_sys::GTRUE, - "{} message", - "Dynamic" - ); + let true_dynamic_res = glib_result_from_gboolean!(glib_sys::GTRUE, "{} message", "Dynamic"); assert!(true_dynamic_res.is_ok()); - } + } } diff --git a/src/gobject/auto/binding.rs b/src/gobject/auto/binding.rs index 49b376cc..96e3606c 100644 --- a/src/gobject/auto/binding.rs +++ b/src/gobject/auto/binding.rs @@ -2,12 +2,12 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT -use BindingFlags; -use GString; -use Object; use gobject_sys; use std::fmt; use translate::*; +use BindingFlags; +use GString; +use Object; glib_wrapper! { pub struct Binding(Object); @@ -19,32 +19,30 @@ glib_wrapper! { impl Binding { pub fn get_flags(&self) -> BindingFlags { - unsafe { - from_glib(gobject_sys::g_binding_get_flags(self.to_glib_none().0)) - } + unsafe { from_glib(gobject_sys::g_binding_get_flags(self.to_glib_none().0)) } } pub fn get_source(&self) -> Option { - unsafe { - from_glib_none(gobject_sys::g_binding_get_source(self.to_glib_none().0)) - } + unsafe { from_glib_none(gobject_sys::g_binding_get_source(self.to_glib_none().0)) } } pub fn get_source_property(&self) -> Option { unsafe { - from_glib_none(gobject_sys::g_binding_get_source_property(self.to_glib_none().0)) + from_glib_none(gobject_sys::g_binding_get_source_property( + self.to_glib_none().0, + )) } } pub fn get_target(&self) -> Option { - unsafe { - from_glib_none(gobject_sys::g_binding_get_target(self.to_glib_none().0)) - } + unsafe { from_glib_none(gobject_sys::g_binding_get_target(self.to_glib_none().0)) } } pub fn get_target_property(&self) -> Option { unsafe { - from_glib_none(gobject_sys::g_binding_get_target_property(self.to_glib_none().0)) + from_glib_none(gobject_sys::g_binding_get_target_property( + self.to_glib_none().0, + )) } } diff --git a/src/gobject/auto/flags.rs b/src/gobject/auto/flags.rs index 458200fa..8069a1a7 100644 --- a/src/gobject/auto/flags.rs +++ b/src/gobject/auto/flags.rs @@ -2,14 +2,14 @@ // from gir-files (https://github.com/gtk-rs/gir-files) // DO NOT EDIT -use StaticType; -use Type; use gobject_sys; use translate::*; use value::FromValue; use value::FromValueOptional; use value::SetValue; use value::Value; +use StaticType; +use Type; bitflags! { pub struct BindingFlags: u32 { @@ -122,4 +122,3 @@ impl FromGlib for SignalFlags { SignalFlags::from_bits_truncate(value) } } - diff --git a/src/gobject/auto/mod.rs b/src/gobject/auto/mod.rs index b0fc18e5..593df65d 100644 --- a/src/gobject/auto/mod.rs +++ b/src/gobject/auto/mod.rs @@ -11,5 +11,4 @@ pub use self::flags::ParamFlags; pub use self::flags::SignalFlags; #[doc(hidden)] -pub mod traits { -} +pub mod traits {} diff --git a/src/gstring.rs b/src/gstring.rs index f61f57f3..5261da4f 100644 --- a/src/gstring.rs +++ b/src/gstring.rs @@ -43,14 +43,15 @@ impl GString { GString::Borrowed(ptr, length) => unsafe { let bytes = slice::from_raw_parts(*ptr as *const u8, length + 1); CStr::from_bytes_with_nul_unchecked(bytes) - } + }, GString::Owned(ptr, length) => unsafe { let bytes = slice::from_raw_parts(*ptr as *const u8, length + 1); CStr::from_bytes_with_nul_unchecked(bytes) - } - GString::ForeignOwned(cstring) => { - cstring.as_ref().expect("ForeignOwned shouldn't be empty").as_c_str() - } + }, + GString::ForeignOwned(cstring) => cstring + .as_ref() + .expect("ForeignOwned shouldn't be empty") + .as_c_str(), }; cstr.to_str().unwrap() } @@ -200,7 +201,11 @@ impl From for String { #[inline] fn from(mut s: GString) -> Self { if let GString::ForeignOwned(ref mut cstring) = s { - if let Ok(s) = cstring.take().expect("ForeignOwned shouldn't be empty").into_string() { + if let Ok(s) = cstring + .take() + .expect("ForeignOwned shouldn't be empty") + .into_string() + { return s; } } @@ -382,9 +387,7 @@ impl StaticType for GString { impl StaticType for Vec { fn static_type() -> Type { - unsafe { - from_glib(glib_sys::g_strv_get_type()) - } + unsafe { from_glib(glib_sys::g_strv_get_type()) } } } @@ -417,9 +420,9 @@ impl_from_glib_container_as_vec_string!(GString, *mut c_char); #[cfg(test)] mod tests { + use glib_sys; use gstring::GString; use std::ffi::CString; - use glib_sys; #[test] fn test_gstring() { diff --git a/src/key_file.rs b/src/key_file.rs index e4dcb79a..d9568e22 100644 --- a/src/key_file.rs +++ b/src/key_file.rs @@ -19,7 +19,11 @@ impl KeyFile { pub fn save_to_file>(&self, filename: T) -> Result<(), Error> { unsafe { let mut error = ptr::null_mut(); - let _ = glib_sys::g_key_file_save_to_file(self.to_glib_none().0, filename.as_ref().to_glib_none().0, &mut error); + let _ = glib_sys::g_key_file_save_to_file( + self.to_glib_none().0, + filename.as_ref().to_glib_none().0, + &mut error, + ); if error.is_null() { Ok(()) } else { @@ -28,14 +32,21 @@ impl KeyFile { } } - pub fn load_from_data_dirs>(&self, file: T, flags: KeyFileFlags) -> Result { + pub fn load_from_data_dirs>( + &self, + file: T, + flags: KeyFileFlags, + ) -> Result { unsafe { let mut error = ptr::null_mut(); let mut full_path: *mut libc::c_char = ptr::null_mut(); - let _ = glib_sys::g_key_file_load_from_data_dirs(self.to_glib_none().0, - file.as_ref().to_glib_none().0, - &mut full_path, - flags.to_glib(), &mut error); + let _ = glib_sys::g_key_file_load_from_data_dirs( + self.to_glib_none().0, + file.as_ref().to_glib_none().0, + &mut full_path, + flags.to_glib(), + &mut error, + ); if error.is_null() { let path: GString = from_glib_full(full_path); Ok(path::PathBuf::from(&path)) @@ -45,17 +56,25 @@ impl KeyFile { } } - pub fn load_from_dirs, U: AsRef>(&self, file: T, search_dirs: &[U], - flags: KeyFileFlags) -> Result { + pub fn load_from_dirs, U: AsRef>( + &self, + file: T, + search_dirs: &[U], + flags: KeyFileFlags, + ) -> Result { unsafe { - let search_dirs: Vec<&std::path::Path> = search_dirs.iter().map(AsRef::as_ref).collect(); + let search_dirs: Vec<&std::path::Path> = + search_dirs.iter().map(AsRef::as_ref).collect(); let mut error = ptr::null_mut(); let mut full_path: *mut libc::c_char = ptr::null_mut(); - let _ = glib_sys::g_key_file_load_from_dirs(self.to_glib_none().0, - file.as_ref().to_glib_none().0, - search_dirs.to_glib_none().0, - &mut full_path, - flags.to_glib(), &mut error); + let _ = glib_sys::g_key_file_load_from_dirs( + self.to_glib_none().0, + file.as_ref().to_glib_none().0, + search_dirs.to_glib_none().0, + &mut full_path, + flags.to_glib(), + &mut error, + ); if error.is_null() { let path: GString = from_glib_full(full_path); Ok(path::PathBuf::from(&path)) @@ -67,7 +86,11 @@ impl KeyFile { pub fn to_data(&self) -> GString { unsafe { - let ret = glib_sys::g_key_file_to_data(self.to_glib_none().0, ptr::null_mut(), ptr::null_mut()); + let ret = glib_sys::g_key_file_to_data( + self.to_glib_none().0, + ptr::null_mut(), + ptr::null_mut(), + ); from_glib_full(ret) } } @@ -75,16 +98,34 @@ impl KeyFile { pub fn get_boolean(&self, group_name: &str, key: &str) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_boolean(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(from_glib(ret)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_get_boolean( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(from_glib(ret)) + } else { + Err(from_glib_full(error)) + } } } pub fn has_key(&self, group_name: &str, key: &str) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_has_key(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut error); - if error.is_null() { Ok(from_glib(ret)) } else { Err(from_glib_full(error)) } + let ret = glib_sys::g_key_file_has_key( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut error, + ); + if error.is_null() { + Ok(from_glib(ret)) + } else { + Err(from_glib_full(error)) + } } } @@ -92,11 +133,20 @@ impl KeyFile { unsafe { let mut length = mem::uninitialized(); let mut error = ptr::null_mut(); - let ret = glib_sys::g_key_file_get_boolean_list(self.to_glib_none().0, group_name.to_glib_none().0, key.to_glib_none().0, &mut length, &mut error); + let ret = glib_sys::g_key_file_get_boolean_list( + self.to_glib_none().0, + group_name.to_glib_none().0, + key.to_glib_none().0, + &mut length, + &mut error, + ); if !error.is_null() { return Err(from_glib_full(error)); } - Ok(FromGlibContainer::from_glib_container_num(ret, length as usize)) + Ok(FromGlibContainer::from_glib_container_num( + ret, + length as usize, + )) } } } diff --git a/src/lib.rs b/src/lib.rs index 16b20887..8f609b03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,66 +92,28 @@ pub extern crate gobject_sys; #[cfg(feature = "futures")] pub extern crate futures; -use std::ffi::CStr; pub use byte_array::ByteArray; pub use bytes::Bytes; -pub use string::String; pub use closure::Closure; -pub use error::{Error, BoolError}; +pub use error::{BoolError, Error}; pub use file_error::FileError; pub use object::{ - Cast, - IsA, - IsClassFor, - Object, - ObjectExt, - ObjectClass, - ObjectType, - InitiallyUnowned, - InitiallyUnownedClass, - WeakRef, - SendWeakRef, + Cast, InitiallyUnowned, InitiallyUnownedClass, IsA, IsClassFor, Object, ObjectClass, ObjectExt, + ObjectType, SendWeakRef, WeakRef, }; pub use signal::{ - SignalHandlerId, - signal_handler_block, - signal_handler_disconnect, - signal_handler_unblock, - signal_stop_emission_by_name + signal_handler_block, signal_handler_disconnect, signal_handler_unblock, + signal_stop_emission_by_name, SignalHandlerId, }; +use std::ffi::CStr; +pub use string::String; -pub use types::{ - StaticType, - Type, -}; -pub use value::{ - ToValue, - ToSendValue, - TypedValue, - SendValue, - Value, -}; -pub use variant::{ - StaticVariantType, - ToVariant, - Variant, -}; -pub use variant_type::{ - VariantTy, - VariantType, -}; -pub use time_val::{ - TimeVal, - get_current_time, -}; -pub use enums::{ - UserDirectory, - EnumClass, - EnumValue, - FlagsClass, - FlagsValue, - FlagsBuilder, -}; +pub use enums::{EnumClass, EnumValue, FlagsBuilder, FlagsClass, FlagsValue, UserDirectory}; +pub use time_val::{get_current_time, TimeVal}; +pub use types::{StaticType, Type}; +pub use value::{SendValue, ToSendValue, ToValue, TypedValue, Value}; +pub use variant::{StaticVariantType, ToVariant, Variant}; +pub use variant_type::{VariantTy, VariantType}; #[macro_use] pub mod wrapper; @@ -164,8 +126,8 @@ pub mod error; #[macro_use] pub mod object; -pub use auto::*; pub use auto::functions::*; +pub use auto::*; #[allow(clippy::let_and_return)] #[allow(clippy::let_unit_value)] #[allow(clippy::too_many_arguments)] @@ -177,8 +139,8 @@ mod gobject; mod byte_array; mod bytes; -mod string; pub mod char; +mod string; pub use char::*; mod checksum; pub mod closure; @@ -197,12 +159,12 @@ pub use gstring::GString; pub mod types; mod utils; pub use utils::*; +mod main_context; +mod main_context_channel; pub mod value; pub mod variant; mod variant_type; -mod main_context; -mod main_context_channel; -pub use main_context_channel::{Sender, SyncSender, Receiver}; +pub use main_context_channel::{Receiver, Sender, SyncSender}; mod date; pub use date::Date; mod value_array; @@ -213,16 +175,13 @@ mod quark; pub use quark::Quark; pub mod send_unique; -pub use send_unique::{ - SendUniqueCell, - SendUnique, -}; +pub use send_unique::{SendUnique, SendUniqueCell}; -#[cfg(feature="futures")] +#[cfg(feature = "futures")] mod main_context_futures; -#[cfg(feature="futures")] +#[cfg(feature = "futures")] mod source_futures; -#[cfg(feature="futures")] +#[cfg(feature = "futures")] pub use source_futures::*; // Actual thread IDs can be reused by the OS once the old thread finished. @@ -241,5 +200,5 @@ pub(crate) fn get_thread_id() -> usize { } #[macro_use] -#[cfg(any(feature = "dox", feature="subclassing"))] +#[cfg(any(feature = "dox", feature = "subclassing"))] pub mod subclass; diff --git a/src/main_context.rs b/src/main_context.rs index 90b6ef9c..a2d9fb62 100644 --- a/src/main_context.rs +++ b/src/main_context.rs @@ -15,7 +15,10 @@ impl MainContext { unsafe { let mut priority = mem::uninitialized(); - let res = from_glib(glib_sys::g_main_context_prepare(self.to_glib_none().0, &mut priority)); + let res = from_glib(glib_sys::g_main_context_prepare( + self.to_glib_none().0, + &mut priority, + )); (res, priority) } @@ -23,19 +26,26 @@ impl MainContext { pub fn find_source_by_id(&self, source_id: &SourceId) -> Option { unsafe { - from_glib_none(glib_sys::g_main_context_find_source_by_id(self.to_glib_none().0, source_id.to_glib())) + from_glib_none(glib_sys::g_main_context_find_source_by_id( + self.to_glib_none().0, + source_id.to_glib(), + )) } } /// Invokes `func` on the main context. pub fn invoke(&self, func: F) - where F: FnOnce() + Send + 'static { + where + F: FnOnce() + Send + 'static, + { self.invoke_with_priority(::PRIORITY_DEFAULT_IDLE, func); } /// Invokes `func` on the main context with the given priority. pub fn invoke_with_priority(&self, priority: Priority, func: F) - where F: FnOnce() + Send + 'static { + where + F: FnOnce() + Send + 'static, + { unsafe { self.invoke_unsafe(priority, func); } @@ -49,7 +59,9 @@ impl MainContext { /// This function panics if called from a different thread than the one that /// owns the main context. pub fn invoke_local(&self, func: F) - where F: FnOnce() + 'static { + where + F: FnOnce() + 'static, + { self.invoke_local_with_priority(::PRIORITY_DEFAULT_IDLE, func); } @@ -61,7 +73,9 @@ impl MainContext { /// This function panics if called from a different thread than the one that /// owns the main context. pub fn invoke_local_with_priority(&self, priority: Priority, func: F) - where F: FnOnce() + 'static { + where + F: FnOnce() + 'static, + { unsafe { assert!(self.is_owner()); self.invoke_unsafe(priority, func); @@ -69,10 +83,17 @@ impl MainContext { } unsafe fn invoke_unsafe(&self, priority: Priority, func: F) - where F: FnOnce() + 'static { + where + F: FnOnce() + 'static, + { let func = Box::into_raw(Box::new(Some(func))); - glib_sys::g_main_context_invoke_full(self.to_glib_none().0, priority.to_glib(), Some(trampoline::), - func as gpointer, Some(destroy_closure::)) + glib_sys::g_main_context_invoke_full( + self.to_glib_none().0, + priority.to_glib(), + Some(trampoline::), + func as gpointer, + Some(destroy_closure::), + ) } /// Calls closure with context configured as the thread default one. @@ -85,7 +106,9 @@ impl MainContext { /// [push_thread_default]: struct.MainContext.html#method.push_thread_default /// [pop_thread_default]: struct.MainContext.html#method.pop_thread_default pub fn with_thread_default(&self, func: F) -> R - where F: FnOnce() -> R { + where + F: FnOnce() -> R, + { let _thread_default = ThreadDefaultContext::new(self); func() } @@ -93,7 +116,9 @@ impl MainContext { unsafe extern "C" fn trampoline(func: gpointer) -> gboolean { let func: &mut Option = &mut *(func as *mut Option); - let func = func.take().expect("MainContext::invoke() closure called multiple times"); + let func = func + .take() + .expect("MainContext::invoke() closure called multiple times"); func(); glib_sys::G_SOURCE_REMOVE } @@ -102,7 +127,6 @@ unsafe extern "C" fn destroy_closure(ptr: gpointer) { Box::>::from_raw(ptr as *mut _); } - struct ThreadDefaultContext<'a>(&'a MainContext); impl<'a> ThreadDefaultContext<'a> { @@ -184,6 +208,5 @@ mod tests { let t = MainContext::get_thread_default().unwrap(); assert!(is_same_context(&a, &t)); }); - } } diff --git a/src/object.rs b/src/object.rs index 434e44dc..7307f4db 100644 --- a/src/object.rs +++ b/src/object.rs @@ -31,11 +31,20 @@ pub use gobject_sys::GObject; pub use gobject_sys::GObjectClass; /// Implemented by types representing `glib::Object` and subclasses of it. -pub unsafe trait ObjectType: UnsafeFrom + Into - + StaticType - + fmt::Debug + Clone + PartialEq + Eq + PartialOrd + Ord + hash::Hash - + for<'a> ToGlibPtr<'a, *mut ::GlibType> - + 'static { +pub unsafe trait ObjectType: + UnsafeFrom + + Into + + StaticType + + fmt::Debug + + Clone + + PartialEq + + Eq + + PartialOrd + + Ord + + hash::Hash + + for<'a> ToGlibPtr<'a, *mut ::GlibType> + + 'static +{ /// type of the FFI Instance structure. type GlibType: 'static; /// type of the FFI Class structure. @@ -63,7 +72,7 @@ pub trait UnsafeFrom { /// /// The trait can only be implemented if the appropriate `ToGlibPtr` /// implementations exist. -pub unsafe trait IsA: ObjectType + AsRef + 'static { } +pub unsafe trait IsA: ObjectType + AsRef + 'static {} /// Trait for mapping a class struct type to its corresponding instance type. pub unsafe trait IsClassFor: Sized + 'static { @@ -80,8 +89,9 @@ pub unsafe trait IsClassFor: Sized + 'static { /// Casts this class to a reference to a parent type's class. fn upcast_ref(&self) -> &U - where Self::Instance: IsA, - U::Instance: ObjectType + where + Self::Instance: IsA, + U::Instance: ObjectType, { unsafe { let klass = self as *const _ as *const U; @@ -91,8 +101,9 @@ pub unsafe trait IsClassFor: Sized + 'static { /// Casts this class to a mutable reference to a parent type's class. fn upcast_ref_mut(&mut self) -> &mut U - where Self::Instance: IsA, - U::Instance: ObjectType + where + Self::Instance: IsA, + U::Instance: ObjectType, { unsafe { let klass = self as *mut _ as *mut U; @@ -103,8 +114,9 @@ pub unsafe trait IsClassFor: Sized + 'static { /// Casts this class to a reference to a child type's class or /// fails if this class is not implementing the child class. fn downcast_ref(&self) -> Option<&U> - where U::Instance: IsA, - Self::Instance: ObjectType + where + U::Instance: IsA, + Self::Instance: ObjectType, { if !self.get_type().is_a(&U::Instance::static_type()) { return None; @@ -119,8 +131,9 @@ pub unsafe trait IsClassFor: Sized + 'static { /// Casts this class to a mutable reference to a child type's class or /// fails if this class is not implementing the child class. fn downcast_ref_mut(&mut self) -> Option<&mut U> - where U::Instance: IsA, - Self::Instance: ObjectType + where + U::Instance: IsA, + Self::Instance: ObjectType, { if !self.get_type().is_a(&U::Instance::static_type()) { return None; @@ -136,18 +149,18 @@ pub unsafe trait IsClassFor: Sized + 'static { /// /// This will return `None` if `type_` is not a subclass of `Self`. fn from_type(type_: Type) -> Option> { - if !type_.is_a(&Self::Instance::static_type()) { - return None; - } + if !type_.is_a(&Self::Instance::static_type()) { + return None; + } - unsafe { - let ptr = gobject_sys::g_type_class_ref(type_.to_glib()); - if ptr.is_null() { - None - } else { - Some(ClassRef(ptr::NonNull::new_unchecked(ptr as *mut Self))) + unsafe { + let ptr = gobject_sys::g_type_class_ref(type_.to_glib()); + if ptr.is_null() { + None + } else { + Some(ClassRef(ptr::NonNull::new_unchecked(ptr as *mut Self))) + } } - } } } @@ -158,9 +171,7 @@ impl ops::Deref for ClassRef { type Target = T; fn deref(&self) -> &T { - unsafe { - self.0.as_ref() - } + unsafe { self.0.as_ref() } } } @@ -191,10 +202,10 @@ pub trait Cast: ObjectType { /// ``` #[inline] fn upcast(self) -> T - where Self: IsA { - unsafe { - self.unsafe_cast() - } + where + Self: IsA, + { + unsafe { self.unsafe_cast() } } /// Upcasts an object to a reference of its superclass or interface `T`. @@ -212,10 +223,10 @@ pub trait Cast: ObjectType { /// ``` #[inline] fn upcast_ref(&self) -> &T - where Self: IsA { - unsafe { - self.unsafe_cast_ref() - } + where + Self: IsA, + { + unsafe { self.unsafe_cast_ref() } } /// Tries to downcast to a subclass or interface implementor `T`. @@ -237,7 +248,9 @@ pub trait Cast: ObjectType { /// ``` #[inline] fn downcast(self) -> Result - where Self: CanDowncast { + where + Self: CanDowncast, + { if self.is::() { Ok(unsafe { self.unsafe_cast() }) } else { @@ -264,7 +277,9 @@ pub trait Cast: ObjectType { /// ``` #[inline] fn downcast_ref(&self) -> Option<&T> - where Self: CanDowncast { + where + Self: CanDowncast, + { if self.is::() { Some(unsafe { self.unsafe_cast_ref() }) } else { @@ -349,16 +364,16 @@ pub trait Cast: ObjectType { // same representation except for the name and the phantom data // type. IsA<> is an unsafe trait that must only be implemented // if this is a valid wrapper type - &*(self as *const Self as *const T) + &*(self as *const Self as *const T) } } -impl Cast for T { } +impl Cast for T {} /// Marker trait for the statically known possibility of downcasting from `Self` to `T`. -pub trait CanDowncast { } +pub trait CanDowncast {} -impl, Sub: IsA> CanDowncast for Super { } +impl, Sub: IsA> CanDowncast for Super {} glib_wrapper! { #[doc(hidden)] @@ -865,21 +880,25 @@ impl Object { return Err(glib_bool_error!("Can't instantiate non-GObject objects")); } - let params = properties.iter() - .map(|&(name, value)| - (CString::new(name).unwrap(), value.to_value())) - .collect::>(); + let params = properties + .iter() + .map(|&(name, value)| (CString::new(name).unwrap(), value.to_value())) + .collect::>(); - let params_c = params.iter() - .map(|&(ref name, ref value)| - gobject_sys::GParameter { - name: name.as_ptr(), - value: unsafe { *value.to_glib_none().0 } - }) - .collect::>(); + let params_c = params + .iter() + .map(|&(ref name, ref value)| gobject_sys::GParameter { + name: name.as_ptr(), + value: unsafe { *value.to_glib_none().0 }, + }) + .collect::>(); unsafe { - let ptr = gobject_sys::g_object_newv(type_.to_glib(), params_c.len() as u32, mut_override(params_c.as_ptr())); + let ptr = gobject_sys::g_object_newv( + type_.to_glib(), + params_c.len() as u32, + mut_override(params_c.as_ptr()), + ); if ptr.is_null() { Err(glib_bool_error!("Can't instantiate object")) } else if type_.is_a(&InitiallyUnowned::static_type()) { @@ -898,9 +917,17 @@ pub trait ObjectExt: ObjectType { fn get_type(&self) -> Type; fn get_object_class(&self) -> &ObjectClass; - fn set_property<'a, N: Into<&'a str>>(&self, property_name: N, value: &dyn ToValue) -> Result<(), BoolError>; + fn set_property<'a, N: Into<&'a str>>( + &self, + property_name: N, + value: &dyn ToValue, + ) -> Result<(), BoolError>; fn get_property<'a, N: Into<&'a str>>(&self, property_name: N) -> Result; - fn has_property<'a, N: Into<&'a str>>(&self, property_name: N, type_: Option) -> Result<(), BoolError>; + fn has_property<'a, N: Into<&'a str>>( + &self, + property_name: N, + type_: Option, + ) -> Result<(), BoolError>; fn get_property_type<'a, N: Into<&'a str>>(&self, property_name: N) -> Option; fn find_property<'a, N: Into<&'a str>>(&self, property_name: N) -> Option<::ParamSpec>; fn list_properties(&self) -> Vec<::ParamSpec>; @@ -909,21 +936,52 @@ pub trait ObjectExt: ObjectType { fn unblock_signal(&self, handler_id: &SignalHandlerId); fn stop_signal_emission(&self, signal_name: &str); - fn connect<'a, N, F>(&self, signal_name: N, after: bool, callback: F) -> Result - where N: Into<&'a str>, F: Fn(&[Value]) -> Option + Send + Sync + 'static; - unsafe fn connect_unsafe<'a, N, F>(&self, signal_name: N, after: bool, callback: F) -> Result - where N: Into<&'a str>, F: Fn(&[Value]) -> Option; - fn emit<'a, N: Into<&'a str>>(&self, signal_name: N, args: &[&dyn ToValue]) -> Result, BoolError>; + fn connect<'a, N, F>( + &self, + signal_name: N, + after: bool, + callback: F, + ) -> Result + where + N: Into<&'a str>, + F: Fn(&[Value]) -> Option + Send + Sync + 'static; + unsafe fn connect_unsafe<'a, N, F>( + &self, + signal_name: N, + after: bool, + callback: F, + ) -> Result + where + N: Into<&'a str>, + F: Fn(&[Value]) -> Option; + fn emit<'a, N: Into<&'a str>>( + &self, + signal_name: N, + args: &[&dyn ToValue], + ) -> Result, BoolError>; fn disconnect(&self, handler_id: SignalHandlerId); - fn connect_notify(&self, name: Option<&str>, f: F) -> SignalHandlerId; - unsafe fn connect_notify_unsafe(&self, name: Option<&str>, f: F) -> SignalHandlerId; + fn connect_notify( + &self, + name: Option<&str>, + f: F, + ) -> SignalHandlerId; + unsafe fn connect_notify_unsafe( + &self, + name: Option<&str>, + f: F, + ) -> SignalHandlerId; fn notify<'a, N: Into<&'a str>>(&self, property_name: N); fn notify_by_pspec(&self, pspec: &::ParamSpec); fn downgrade(&self) -> WeakRef; - fn bind_property<'a, O: ObjectType, N: Into<&'a str>, M: Into<&'a str>>(&'a self, source_property: N, target: &'a O, target_property: M) -> BindingBuilder<'a>; + fn bind_property<'a, O: ObjectType, N: Into<&'a str>, M: Into<&'a str>>( + &'a self, + source_property: N, + target: &'a O, + target_property: M, + ) -> BindingBuilder<'a>; fn ref_count(&self) -> u32; } @@ -945,7 +1003,11 @@ impl ObjectExt for T { } } - fn set_property<'a, N: Into<&'a str>>(&self, property_name: N, value: &dyn ToValue) -> Result<(), BoolError> { + fn set_property<'a, N: Into<&'a str>>( + &self, + property_name: N, + value: &dyn ToValue, + ) -> Result<(), BoolError> { let property_name = property_name.into(); let mut property_value = value.to_value(); @@ -956,7 +1018,9 @@ impl ObjectExt for T { } }; - if !pspec.get_flags().contains(::ParamFlags::WRITABLE) || pspec.get_flags().contains(::ParamFlags::CONSTRUCT_ONLY) { + if !pspec.get_flags().contains(::ParamFlags::WRITABLE) + || pspec.get_flags().contains(::ParamFlags::CONSTRUCT_ONLY) + { return Err(glib_bool_error!("property is not writable")); } @@ -966,8 +1030,9 @@ impl ObjectExt for T { // with Rust's type rules. We only allow the exact same type, or if the // value type is a subtype of the property type let valid_type: bool = from_glib(gobject_sys::g_type_check_value_holds( - mut_override(property_value.to_glib_none().0), - pspec.get_value_type().to_glib())); + mut_override(property_value.to_glib_none().0), + pspec.get_value_type().to_glib(), + )); // If it's not directly a valid type but an object type, we check if the // actual type of the contained object is compatible and if so create @@ -978,18 +1043,24 @@ impl ObjectExt for T { if obj.get_type().is_a(&pspec.get_value_type()) { property_value.0.g_type = pspec.get_value_type().to_glib(); } else { - return Err(glib_bool_error!("property can't be set from the given object type")); + return Err(glib_bool_error!( + "property can't be set from the given object type" + )); } } else { // Otherwise if the value is None then the type is compatible too property_value.0.g_type = pspec.get_value_type().to_glib(); } } else if !valid_type { - return Err(glib_bool_error!("property can't be set from the given type")); + return Err(glib_bool_error!( + "property can't be set from the given type" + )); } let changed: bool = from_glib(gobject_sys::g_param_value_validate( - pspec.to_glib_none().0, property_value.to_glib_none_mut().0)); + pspec.to_glib_none().0, + property_value.to_glib_none_mut().0, + )); let change_allowed = pspec.get_flags().contains(::ParamFlags::LAX_VALIDATION); if changed && !change_allowed { return Err(glib_bool_error!( @@ -997,9 +1068,11 @@ impl ObjectExt for T { )); } - gobject_sys::g_object_set_property(self.as_object_ref().to_glib_none().0, - property_name.to_glib_none().0, - property_value.to_glib_none().0); + gobject_sys::g_object_set_property( + self.as_object_ref().to_glib_none().0, + property_name.to_glib_none().0, + property_value.to_glib_none().0, + ); } Ok(()) @@ -1021,7 +1094,11 @@ impl ObjectExt for T { unsafe { let mut value = Value::from_type(pspec.get_value_type()); - gobject_sys::g_object_get_property(self.as_object_ref().to_glib_none().0, property_name.to_glib_none().0, value.to_glib_none_mut().0); + gobject_sys::g_object_get_property( + self.as_object_ref().to_glib_none().0, + property_name.to_glib_none().0, + value.to_glib_none_mut().0, + ); // This can't really happen unless something goes wrong inside GObject if value.type_() == ::Type::Invalid { @@ -1034,39 +1111,65 @@ impl ObjectExt for T { fn block_signal(&self, handler_id: &SignalHandlerId) { unsafe { - gobject_sys::g_signal_handler_block(self.as_object_ref().to_glib_none().0, handler_id.to_glib()); + gobject_sys::g_signal_handler_block( + self.as_object_ref().to_glib_none().0, + handler_id.to_glib(), + ); } } fn unblock_signal(&self, handler_id: &SignalHandlerId) { unsafe { - gobject_sys::g_signal_handler_unblock(self.as_object_ref().to_glib_none().0, handler_id.to_glib()); + gobject_sys::g_signal_handler_unblock( + self.as_object_ref().to_glib_none().0, + handler_id.to_glib(), + ); } } fn stop_signal_emission(&self, signal_name: &str) { unsafe { - gobject_sys::g_signal_stop_emission_by_name(self.as_object_ref().to_glib_none().0, signal_name.to_glib_none().0); + gobject_sys::g_signal_stop_emission_by_name( + self.as_object_ref().to_glib_none().0, + signal_name.to_glib_none().0, + ); } } fn disconnect(&self, handler_id: SignalHandlerId) { unsafe { - gobject_sys::g_signal_handler_disconnect(self.as_object_ref().to_glib_none().0, handler_id.to_glib()); + gobject_sys::g_signal_handler_disconnect( + self.as_object_ref().to_glib_none().0, + handler_id.to_glib(), + ); } } - fn connect_notify(&self, name: Option<&str>, f: F) -> SignalHandlerId { - unsafe { - self.connect_notify_unsafe(name, f) - } + fn connect_notify( + &self, + name: Option<&str>, + f: F, + ) -> SignalHandlerId { + unsafe { self.connect_notify_unsafe(name, f) } } - unsafe fn connect_notify_unsafe(&self, name: Option<&str>, f: F) -> SignalHandlerId { - unsafe extern "C" fn notify_trampoline(this: *mut gobject_sys::GObject, param_spec: *mut gobject_sys::GParamSpec, f: glib_sys::gpointer) - where P: ObjectType { + unsafe fn connect_notify_unsafe( + &self, + name: Option<&str>, + f: F, + ) -> SignalHandlerId { + unsafe extern "C" fn notify_trampoline( + this: *mut gobject_sys::GObject, + param_spec: *mut gobject_sys::GParamSpec, + f: glib_sys::gpointer, + ) where + P: ObjectType, + { let f: &F = &*(f as *const F); - f(&Object::from_glib_borrow(this).unsafe_cast(), &from_glib_borrow(param_spec)) + f( + &Object::from_glib_borrow(this).unsafe_cast(), + &from_glib_borrow(param_spec), + ) } let signal_name = if let Some(name) = name { @@ -1076,25 +1179,39 @@ impl ObjectExt for T { }; let f: Box = Box::new(f); - ::signal::connect_raw(self.as_object_ref().to_glib_none().0, signal_name.as_ptr() as *const _, - Some(mem::transmute(notify_trampoline:: as usize)), Box::into_raw(f)) + ::signal::connect_raw( + self.as_object_ref().to_glib_none().0, + signal_name.as_ptr() as *const _, + Some(mem::transmute(notify_trampoline:: as usize)), + Box::into_raw(f), + ) } fn notify<'a, N: Into<&'a str>>(&self, property_name: N) { let property_name = property_name.into(); unsafe { - gobject_sys::g_object_notify(self.as_object_ref().to_glib_none().0, property_name.to_glib_none().0); + gobject_sys::g_object_notify( + self.as_object_ref().to_glib_none().0, + property_name.to_glib_none().0, + ); } } fn notify_by_pspec(&self, pspec: &::ParamSpec) { unsafe { - gobject_sys::g_object_notify_by_pspec(self.as_object_ref().to_glib_none().0, pspec.to_glib_none().0); + gobject_sys::g_object_notify_by_pspec( + self.as_object_ref().to_glib_none().0, + pspec.to_glib_none().0, + ); } } - fn has_property<'a, N: Into<&'a str>>(&self, property_name: N, type_: Option) -> Result<(), BoolError> { + fn has_property<'a, N: Into<&'a str>>( + &self, + property_name: N, + type_: Option, + ) -> Result<(), BoolError> { self.get_object_class().has_property(property_name, type_) } @@ -1110,15 +1227,29 @@ impl ObjectExt for T { self.get_object_class().list_properties() } - fn connect<'a, N, F>(&self, signal_name: N, after: bool, callback: F) -> Result - where N: Into<&'a str>, F: Fn(&[Value]) -> Option + Send + Sync + 'static { - unsafe { - self.connect_unsafe(signal_name, after, callback) - } + fn connect<'a, N, F>( + &self, + signal_name: N, + after: bool, + callback: F, + ) -> Result + where + N: Into<&'a str>, + F: Fn(&[Value]) -> Option + Send + Sync + 'static, + { + unsafe { self.connect_unsafe(signal_name, after, callback) } } - unsafe fn connect_unsafe<'a, N, F>(&self, signal_name: N, after: bool, callback: F) -> Result - where N: Into<&'a str>, F: Fn(&[Value]) -> Option { + unsafe fn connect_unsafe<'a, N, F>( + &self, + signal_name: N, + after: bool, + callback: F, + ) -> Result + where + N: Into<&'a str>, + F: Fn(&[Value]) -> Option, + { let signal_name: &str = signal_name.into(); let type_ = self.get_type(); @@ -1126,9 +1257,13 @@ impl ObjectExt for T { let mut signal_id = 0; let mut signal_detail = 0; - let found: bool = from_glib(gobject_sys::g_signal_parse_name(signal_name.to_glib_none().0, - type_.to_glib(), &mut signal_id, - &mut signal_detail, true.to_glib())); + let found: bool = from_glib(gobject_sys::g_signal_parse_name( + signal_name.to_glib_none().0, + type_.to_glib(), + &mut signal_id, + &mut signal_detail, + true.to_glib(), + )); if !found { return Err(glib_bool_error!("Signal not found")); @@ -1141,52 +1276,68 @@ impl ObjectExt for T { } // This is actually G_SIGNAL_TYPE_STATIC_SCOPE - let return_type: Type = from_glib(details.return_type & (!gobject_sys::G_TYPE_FLAG_RESERVED_ID_BIT)); + let return_type: Type = + from_glib(details.return_type & (!gobject_sys::G_TYPE_FLAG_RESERVED_ID_BIT)); let closure = Closure::new_unsafe(move |values| { let ret = callback(values); if return_type == Type::Unit { if let Some(ret) = ret { - panic!("Signal required no return value but got value of type {}", ret.type_().name()); + panic!( + "Signal required no return value but got value of type {}", + ret.type_().name() + ); } None } else { match ret { Some(mut ret) => { let valid_type: bool = from_glib(gobject_sys::g_type_check_value_holds( - mut_override(ret.to_glib_none().0), - return_type.to_glib())); - - // If it's not directly a valid type but an object type, we check if the - // actual typed of the contained object is compatible and if so create - // a properly typed Value. This can happen if the type field in the - // Value is set to a more generic type than the contained value - if !valid_type && ret.type_().is_a(&Object::static_type()) { - if let Some(obj) = ret.get::() { - if obj.get_type().is_a(&return_type) { - ret.0.g_type = return_type.to_glib(); - } else { - panic!("Signal required return value of type {} but got {} (actual {})", + mut_override(ret.to_glib_none().0), + return_type.to_glib(), + )); + + // If it's not directly a valid type but an object type, we check if the + // actual typed of the contained object is compatible and if so create + // a properly typed Value. This can happen if the type field in the + // Value is set to a more generic type than the contained value + if !valid_type && ret.type_().is_a(&Object::static_type()) { + if let Some(obj) = ret.get::() { + if obj.get_type().is_a(&return_type) { + ret.0.g_type = return_type.to_glib(); + } else { + panic!("Signal required return value of type {} but got {} (actual {})", return_type.name(), ret.type_().name(), obj.get_type().name()); + } + } else { + // Otherwise if the value is None then the type is compatible too + ret.0.g_type = return_type.to_glib(); } - } else { - // Otherwise if the value is None then the type is compatible too - ret.0.g_type = return_type.to_glib(); - } - } else if !valid_type { - panic!("Signal required return value of type {} but got {}", - return_type.name(), ret.type_().name()); + } else if !valid_type { + panic!( + "Signal required return value of type {} but got {}", + return_type.name(), + ret.type_().name() + ); } Some(ret) } None => { - panic!("Signal required return value of type {} but got None", return_type.name()); + panic!( + "Signal required return value of type {} but got None", + return_type.name() + ); } } } }); - let handler = gobject_sys::g_signal_connect_closure_by_id(self.as_object_ref().to_glib_none().0, signal_id, signal_detail, - closure.to_glib_none().0, after.to_glib()); + let handler = gobject_sys::g_signal_connect_closure_by_id( + self.as_object_ref().to_glib_none().0, + signal_id, + signal_detail, + closure.to_glib_none().0, + after.to_glib(), + ); if handler == 0 { Err(glib_bool_error!("Failed to connect to signal")) @@ -1195,7 +1346,11 @@ impl ObjectExt for T { } } - fn emit<'a, N: Into<&'a str>>(&self, signal_name: N, args: &[&dyn ToValue]) -> Result, BoolError> { + fn emit<'a, N: Into<&'a str>>( + &self, + signal_name: N, + args: &[&dyn ToValue], + ) -> Result, BoolError> { let signal_name: &str = signal_name.into(); unsafe { let type_ = self.get_type(); @@ -1203,9 +1358,13 @@ impl ObjectExt for T { let mut signal_id = 0; let mut signal_detail = 0; - let found: bool = from_glib(gobject_sys::g_signal_parse_name(signal_name.to_glib_none().0, - type_.to_glib(), &mut signal_id, - &mut signal_detail, true.to_glib())); + let found: bool = from_glib(gobject_sys::g_signal_parse_name( + signal_name.to_glib_none().0, + type_.to_glib(), + &mut signal_id, + &mut signal_detail, + true.to_glib(), + )); if !found { return Err(glib_bool_error!("Signal not found")); @@ -1222,7 +1381,8 @@ impl ObjectExt for T { } for (i, item) in args.iter().enumerate() { - let arg_type = *(details.param_types.add(i)) & (!gobject_sys::G_TYPE_FLAG_RESERVED_ID_BIT); + let arg_type = + *(details.param_types.add(i)) & (!gobject_sys::G_TYPE_FLAG_RESERVED_ID_BIT); if arg_type != item.to_value_type().to_glib() { return Err(glib_bool_error!("Incompatible argument types")); } @@ -1233,13 +1393,16 @@ impl ObjectExt for T { let self_v = { let mut v = Value::uninitialized(); gobject_sys::g_value_init(v.to_glib_none_mut().0, self.get_type().to_glib()); - gobject_sys::g_value_set_object(v.to_glib_none_mut().0, self.as_object_ref().to_glib_none().0); + gobject_sys::g_value_set_object( + v.to_glib_none_mut().0, + self.as_object_ref().to_glib_none().0, + ); v }; let args = if args.len() < 10 { s_args[0] = self_v; for (i, arg) in args.iter().enumerate() { - s_args[i+1] = arg.to_value(); + s_args[i + 1] = arg.to_value(); } &s_args[0..=args.len()] } else { @@ -1256,8 +1419,12 @@ impl ObjectExt for T { gobject_sys::g_value_init(return_value.to_glib_none_mut().0, details.return_type); } - gobject_sys::g_signal_emitv(mut_override(args.as_ptr()) as *mut gobject_sys::GValue, - signal_id, signal_detail, return_value.to_glib_none_mut().0); + gobject_sys::g_signal_emitv( + mut_override(args.as_ptr()) as *mut gobject_sys::GValue, + signal_id, + signal_detail, + return_value.to_glib_none_mut().0, + ); if return_value.type_() != Type::Unit && return_value.type_() != Type::Invalid { Ok(Some(return_value)) @@ -1270,12 +1437,20 @@ impl ObjectExt for T { fn downgrade(&self) -> WeakRef { unsafe { let w = WeakRef(Box::new(mem::uninitialized()), PhantomData); - gobject_sys::g_weak_ref_init(mut_override(&*w.0), self.as_object_ref().to_glib_none().0); + gobject_sys::g_weak_ref_init( + mut_override(&*w.0), + self.as_object_ref().to_glib_none().0, + ); w } } - fn bind_property<'a, O: ObjectType, N: Into<&'a str>, M: Into<&'a str>>(&'a self, source_property: N, target: &'a O, target_property: M) -> BindingBuilder<'a> { + fn bind_property<'a, O: ObjectType, N: Into<&'a str>, M: Into<&'a str>>( + &'a self, + source_property: N, + target: &'a O, + target_property: M, + ) -> BindingBuilder<'a> { let source_property = source_property.into(); let target_property = target_property.into(); @@ -1291,7 +1466,11 @@ impl ObjectExt for T { } impl ObjectClass { - pub fn has_property<'a, N: Into<&'a str>>(&self, property_name: N, type_: Option) -> Result<(), BoolError> { + pub fn has_property<'a, N: Into<&'a str>>( + &self, + property_name: N, + type_: Option, + ) -> Result<(), BoolError> { let property_name = property_name.into(); let ptype = self.get_property_type(property_name); @@ -1304,12 +1483,13 @@ impl ObjectClass { } else { Err(glib_bool_error!("Invalid property type")) } - }, + } } } pub fn get_property_type<'a, N: Into<&'a str>>(&self, property_name: N) -> Option { - self.find_property(property_name).map(|pspec| pspec.get_value_type()) + self.find_property(property_name) + .map(|pspec| pspec.get_value_type()) } pub fn find_property<'a, N: Into<&'a str>>(&self, property_name: N) -> Option<::ParamSpec> { @@ -1317,7 +1497,10 @@ impl ObjectClass { unsafe { let klass = self as *const _ as *const gobject_sys::GObjectClass; - from_glib_none(gobject_sys::g_object_class_find_property(klass as *mut _, property_name.to_glib_none().0)) + from_glib_none(gobject_sys::g_object_class_find_property( + klass as *mut _, + property_name.to_glib_none().0, + )) } } @@ -1327,7 +1510,8 @@ impl ObjectClass { let mut n_properties = 0; - let props = gobject_sys::g_object_class_list_properties(klass as *mut _, &mut n_properties); + let props = + gobject_sys::g_object_class_list_properties(klass as *mut _, &mut n_properties); FromGlibContainer::from_glib_none_num(props, n_properties as usize) } } @@ -1465,16 +1649,33 @@ pub struct BindingBuilder<'a> { } impl<'a> BindingBuilder<'a> { - fn new(source: &'a S, source_property: &'a str, target: &'a T, target_property: &'a str) -> Self { - Self { source: source.as_object_ref(), source_property, target: target.as_object_ref(), target_property, flags: ::BindingFlags::DEFAULT, transform_to: None, transform_from: None } + fn new( + source: &'a S, + source_property: &'a str, + target: &'a T, + target_property: &'a str, + ) -> Self { + Self { + source: source.as_object_ref(), + source_property, + target: target.as_object_ref(), + target_property, + flags: ::BindingFlags::DEFAULT, + transform_to: None, + transform_from: None, + } } - fn transform_closure Option + Send + Sync + 'static>(func: F) -> ::Closure { + fn transform_closure Option + Send + Sync + 'static>( + func: F, + ) -> ::Closure { ::Closure::new(move |values| { assert_eq!(values.len(), 3); let binding = values[0].get::<::Binding>().unwrap(); let from = unsafe { - let ptr = gobject_sys::g_value_get_boxed(mut_override(&values[1] as *const Value as *const gobject_sys::GValue)); + let ptr = gobject_sys::g_value_get_boxed(mut_override( + &values[1] as *const Value as *const gobject_sys::GValue, + )); assert!(!ptr.is_null()); &*(ptr as *const gobject_sys::GValue as *const Value) }; @@ -1483,7 +1684,10 @@ impl<'a> BindingBuilder<'a> { None => Some(false.to_value()), Some(value) => { unsafe { - gobject_sys::g_value_set_boxed(mut_override(&values[2] as *const Value as *const gobject_sys::GValue), &value as *const Value as *const _); + gobject_sys::g_value_set_boxed( + mut_override(&values[2] as *const Value as *const gobject_sys::GValue), + &value as *const Value as *const _, + ); } Some(true.to_value()) @@ -1492,14 +1696,20 @@ impl<'a> BindingBuilder<'a> { }) } - pub fn transform_from Option + Send + Sync + 'static>(self, func: F) -> Self { + pub fn transform_from Option + Send + Sync + 'static>( + self, + func: F, + ) -> Self { Self { transform_from: Some(Self::transform_closure(func)), ..self } } - pub fn transform_to Option + Send + Sync + 'static>(self, func: F) -> Self { + pub fn transform_to Option + Send + Sync + 'static>( + self, + func: F, + ) -> Self { Self { transform_to: Some(Self::transform_closure(func)), ..self @@ -1507,25 +1717,20 @@ impl<'a> BindingBuilder<'a> { } pub fn flags(self, flags: ::BindingFlags) -> Self { - Self { - flags, - ..self - } + Self { flags, ..self } } pub fn build(self) -> Option<::Binding> { unsafe { - from_glib_none( - gobject_sys::g_object_bind_property_with_closures( - self.source.to_glib_none().0, - self.source_property.to_glib_none().0, - self.target.to_glib_none().0, - self.target_property.to_glib_none().0, - self.flags.to_glib(), - self.transform_to.to_glib_none().0, - self.transform_from.to_glib_none().0, - ) - ) + from_glib_none(gobject_sys::g_object_bind_property_with_closures( + self.source.to_glib_none().0, + self.source_property.to_glib_none().0, + self.target.to_glib_none().0, + self.target_property.to_glib_none().0, + self.flags.to_glib(), + self.transform_to.to_glib_none().0, + self.transform_from.to_glib_none().0, + )) } } } diff --git a/src/param_spec.rs b/src/param_spec.rs index 7d159875..f75e3513 100644 --- a/src/param_spec.rs +++ b/src/param_spec.rs @@ -26,50 +26,44 @@ unsafe impl Sync for ParamSpec {} impl ParamSpec { pub fn get_value_type(&self) -> ::Type { - unsafe { - from_glib((*self.to_glib_none().0).value_type) - } + unsafe { from_glib((*self.to_glib_none().0).value_type) } } pub fn get_owner_type(&self) -> ::Type { - unsafe { - from_glib((*self.to_glib_none().0).owner_type) - } + unsafe { from_glib((*self.to_glib_none().0).owner_type) } } pub fn get_flags(&self) -> ParamFlags { - unsafe { - from_glib((*self.to_glib_none().0).flags) - } + unsafe { from_glib((*self.to_glib_none().0).flags) } } pub fn get_blurb(&self) -> String { - unsafe { - from_glib_none(gobject_sys::g_param_spec_get_blurb(self.to_glib_none().0)) - } + unsafe { from_glib_none(gobject_sys::g_param_spec_get_blurb(self.to_glib_none().0)) } } pub fn get_default_value(&self) -> Option { unsafe { - from_glib_none(gobject_sys::g_param_spec_get_default_value(self.to_glib_none().0)) + from_glib_none(gobject_sys::g_param_spec_get_default_value( + self.to_glib_none().0, + )) } } pub fn get_name(&self) -> String { - unsafe { - from_glib_none(gobject_sys::g_param_spec_get_name(self.to_glib_none().0)) - } + unsafe { from_glib_none(gobject_sys::g_param_spec_get_name(self.to_glib_none().0)) } } #[cfg(any(feature = "v2_46", feature = "dox"))] pub fn get_name_quark(&self) -> ::Quark { - unsafe { from_glib(gobject_sys::g_param_spec_get_name_quark(self.to_glib_none().0)) } + unsafe { + from_glib(gobject_sys::g_param_spec_get_name_quark( + self.to_glib_none().0, + )) + } } pub fn get_nick(&self) -> String { - unsafe { - from_glib_none(gobject_sys::g_param_spec_get_nick(self.to_glib_none().0)) - } + unsafe { from_glib_none(gobject_sys::g_param_spec_get_nick(self.to_glib_none().0)) } } //pub fn get_qdata(&self, quark: /*Ignored*/glib::Quark) -> /*Unimplemented*/Option { @@ -78,7 +72,9 @@ impl ParamSpec { pub fn get_redirect_target(&self) -> Option { unsafe { - from_glib_none(gobject_sys::g_param_spec_get_redirect_target(self.to_glib_none().0)) + from_glib_none(gobject_sys::g_param_spec_get_redirect_target( + self.to_glib_none().0, + )) } } @@ -94,142 +90,448 @@ impl ParamSpec { // unsafe { TODO: call gobject_sys::g_param_spec_steal_qdata() } //} - pub fn boolean(name: &str, nick: &str, blurb: &str, default_value: bool, flags: ParamFlags) -> ParamSpec { + pub fn boolean( + name: &str, + nick: &str, + blurb: &str, + default_value: bool, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_boolean(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, default_value.to_glib(), flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_boolean( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + default_value.to_glib(), + flags.to_glib(), + )) } } - pub fn boxed(name: &str, nick: &str, blurb: &str, boxed_type: ::Type, flags: ParamFlags) -> ParamSpec { + pub fn boxed( + name: &str, + nick: &str, + blurb: &str, + boxed_type: ::Type, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_boxed(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, boxed_type.to_glib(), flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_boxed( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + boxed_type.to_glib(), + flags.to_glib(), + )) } } - pub fn char(name: &str, nick: &str, blurb: &str, minimum: i8, maximum: i8, default_value: i8, flags: ParamFlags) -> ParamSpec { + pub fn char( + name: &str, + nick: &str, + blurb: &str, + minimum: i8, + maximum: i8, + default_value: i8, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_char(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_char( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn double(name: &str, nick: &str, blurb: &str, minimum: f64, maximum: f64, default_value: f64, flags: ParamFlags) -> ParamSpec { + pub fn double( + name: &str, + nick: &str, + blurb: &str, + minimum: f64, + maximum: f64, + default_value: f64, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_double(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_double( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn enum_(name: &str, nick: &str, blurb: &str, enum_type: ::Type, default_value: i32, flags: ParamFlags) -> ParamSpec { + pub fn enum_( + name: &str, + nick: &str, + blurb: &str, + enum_type: ::Type, + default_value: i32, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_enum(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, enum_type.to_glib(), default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_enum( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + enum_type.to_glib(), + default_value, + flags.to_glib(), + )) } } - pub fn flags(name: &str, nick: &str, blurb: &str, flags_type: ::Type, default_value: u32, flags: ParamFlags) -> ParamSpec { + pub fn flags( + name: &str, + nick: &str, + blurb: &str, + flags_type: ::Type, + default_value: u32, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_flags(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, flags_type.to_glib(), default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_flags( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + flags_type.to_glib(), + default_value, + flags.to_glib(), + )) } } - pub fn float(name: &str, nick: &str, blurb: &str, minimum: f32, maximum: f32, default_value: f32, flags: ParamFlags) -> ParamSpec { + pub fn float( + name: &str, + nick: &str, + blurb: &str, + minimum: f32, + maximum: f32, + default_value: f32, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_float(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_float( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn gtype(name: &str, nick: &str, blurb: &str, is_a_type: ::Type, flags: ParamFlags) -> ParamSpec { + pub fn gtype( + name: &str, + nick: &str, + blurb: &str, + is_a_type: ::Type, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_gtype(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, is_a_type.to_glib(), flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_gtype( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + is_a_type.to_glib(), + flags.to_glib(), + )) } } - pub fn int(name: &str, nick: &str, blurb: &str, minimum: i32, maximum: i32, default_value: i32, flags: ParamFlags) -> ParamSpec { + pub fn int( + name: &str, + nick: &str, + blurb: &str, + minimum: i32, + maximum: i32, + default_value: i32, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_int(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_int( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn int64(name: &str, nick: &str, blurb: &str, minimum: i64, maximum: i64, default_value: i64, flags: ParamFlags) -> ParamSpec { + pub fn int64( + name: &str, + nick: &str, + blurb: &str, + minimum: i64, + maximum: i64, + default_value: i64, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_int64(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_int64( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn long(name: &str, nick: &str, blurb: &str, minimum: libc::c_long, maximum: libc::c_long, default_value: libc::c_long, flags: ParamFlags) -> ParamSpec { + pub fn long( + name: &str, + nick: &str, + blurb: &str, + minimum: libc::c_long, + maximum: libc::c_long, + default_value: libc::c_long, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_long(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_long( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn object(name: &str, nick: &str, blurb: &str, object_type: ::Type, flags: ParamFlags) -> ParamSpec { + pub fn object( + name: &str, + nick: &str, + blurb: &str, + object_type: ::Type, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_object(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, object_type.to_glib(), flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_object( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + object_type.to_glib(), + flags.to_glib(), + )) } } pub fn override_(name: &str, overridden: &ParamSpec) -> ParamSpec { unsafe { - from_glib_none(gobject_sys::g_param_spec_override(name.to_glib_none().0, overridden.to_glib_none().0)) + from_glib_none(gobject_sys::g_param_spec_override( + name.to_glib_none().0, + overridden.to_glib_none().0, + )) } } - pub fn param(name: &str, nick: &str, blurb: &str, param_type: ::Type, flags: ParamFlags) -> ParamSpec { + pub fn param( + name: &str, + nick: &str, + blurb: &str, + param_type: ::Type, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_param(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, param_type.to_glib(), flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_param( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + param_type.to_glib(), + flags.to_glib(), + )) } } pub fn pointer(name: &str, nick: &str, blurb: &str, flags: ParamFlags) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_pointer(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_pointer( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + flags.to_glib(), + )) } } - pub fn string(name: &str, nick: &str, blurb: &str, default_value: Option<&str>, flags: ParamFlags) -> ParamSpec { + pub fn string( + name: &str, + nick: &str, + blurb: &str, + default_value: Option<&str>, + flags: ParamFlags, + ) -> ParamSpec { let default_value = default_value.to_glib_none(); unsafe { - from_glib_full(gobject_sys::g_param_spec_string(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, default_value.0, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_string( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + default_value.0, + flags.to_glib(), + )) } } - pub fn uchar(name: &str, nick: &str, blurb: &str, minimum: u8, maximum: u8, default_value: u8, flags: ParamFlags) -> ParamSpec { + pub fn uchar( + name: &str, + nick: &str, + blurb: &str, + minimum: u8, + maximum: u8, + default_value: u8, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_uchar(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_uchar( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn uint(name: &str, nick: &str, blurb: &str, minimum: u32, maximum: u32, default_value: u32, flags: ParamFlags) -> ParamSpec { + pub fn uint( + name: &str, + nick: &str, + blurb: &str, + minimum: u32, + maximum: u32, + default_value: u32, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_uint(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_uint( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn uint64(name: &str, nick: &str, blurb: &str, minimum: u64, maximum: u64, default_value: u64, flags: ParamFlags) -> ParamSpec { + pub fn uint64( + name: &str, + nick: &str, + blurb: &str, + minimum: u64, + maximum: u64, + default_value: u64, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_uint64(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_uint64( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn ulong(name: &str, nick: &str, blurb: &str, minimum: libc::c_ulong, maximum: libc::c_ulong, default_value: libc::c_ulong, flags: ParamFlags) -> ParamSpec { + pub fn ulong( + name: &str, + nick: &str, + blurb: &str, + minimum: libc::c_ulong, + maximum: libc::c_ulong, + default_value: libc::c_ulong, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_ulong(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, minimum, maximum, default_value, flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_ulong( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + minimum, + maximum, + default_value, + flags.to_glib(), + )) } } - pub fn unichar(name: &str, nick: &str, blurb: &str, default_value: char, flags: ParamFlags) -> ParamSpec { + pub fn unichar( + name: &str, + nick: &str, + blurb: &str, + default_value: char, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_full(gobject_sys::g_param_spec_unichar(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, default_value.to_glib(), flags.to_glib())) + from_glib_full(gobject_sys::g_param_spec_unichar( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + default_value.to_glib(), + flags.to_glib(), + )) } } - pub fn value_array(name: &str, nick: &str, blurb: &str, element_spec: &ParamSpec, flags: ParamFlags) -> ParamSpec { + pub fn value_array( + name: &str, + nick: &str, + blurb: &str, + element_spec: &ParamSpec, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_none(gobject_sys::g_param_spec_value_array(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, element_spec.to_glib_none().0, flags.to_glib())) + from_glib_none(gobject_sys::g_param_spec_value_array( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + element_spec.to_glib_none().0, + flags.to_glib(), + )) } } - pub fn variant(name: &str, nick: &str, blurb: &str, type_: &::VariantTy, default_value: Option<&::Variant>, flags: ParamFlags) -> ParamSpec { + pub fn variant( + name: &str, + nick: &str, + blurb: &str, + type_: &::VariantTy, + default_value: Option<&::Variant>, + flags: ParamFlags, + ) -> ParamSpec { unsafe { - from_glib_none(gobject_sys::g_param_spec_variant(name.to_glib_none().0, nick.to_glib_none().0, blurb.to_glib_none().0, type_.to_glib_none().0, default_value.to_glib_none().0, flags.to_glib())) + from_glib_none(gobject_sys::g_param_spec_variant( + name.to_glib_none().0, + nick.to_glib_none().0, + blurb.to_glib_none().0, + type_.to_glib_none().0, + default_value.to_glib_none().0, + flags.to_glib(), + )) } } } diff --git a/src/prelude.rs b/src/prelude.rs index c840b2fc..2c8f7bb0 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,15 +1,6 @@ //! Traits and essential types intended for blanket imports. pub use { - Cast, - Continue, - IsA, - IsClassFor, - ObjectExt, - ObjectType, - StaticType, - StaticVariantType, - ToValue, - ToSendValue, - ToVariant, + Cast, Continue, IsA, IsClassFor, ObjectExt, ObjectType, StaticType, StaticVariantType, + ToSendValue, ToValue, ToVariant, }; diff --git a/src/quark.rs b/src/quark.rs index 756d0656..6deb8a0f 100644 --- a/src/quark.rs +++ b/src/quark.rs @@ -13,15 +13,15 @@ pub struct Quark(glib_sys::GQuark); impl Quark { pub fn from_string(s: &str) -> Quark { - unsafe { - from_glib(glib_sys::g_quark_from_static_string(s.to_glib_full())) - } + unsafe { from_glib(glib_sys::g_quark_from_static_string(s.to_glib_full())) } } #[allow(clippy::trivially_copy_pass_by_ref)] pub fn to_string(&self) -> &'static str { unsafe { - CStr::from_ptr(glib_sys::g_quark_to_string(self.to_glib())).to_str().unwrap() + CStr::from_ptr(glib_sys::g_quark_to_string(self.to_glib())) + .to_str() + .unwrap() } } diff --git a/src/shared.rs b/src/shared.rs index 9a4b8ab7..b4db8252 100644 --- a/src/shared.rs +++ b/src/shared.rs @@ -292,14 +292,18 @@ pub struct Shared> { impl> Drop for Shared { fn drop(&mut self) { if !self.borrowed { - unsafe { MM::unref(self.inner.as_ptr()); } + unsafe { + MM::unref(self.inner.as_ptr()); + } } } } impl> Clone for Shared { fn clone(&self) -> Self { - unsafe { MM::ref_(self.inner.as_ptr()); } + unsafe { + MM::ref_(self.inner.as_ptr()); + } Shared { inner: self.inner, borrowed: false, @@ -338,13 +342,18 @@ impl> PartialEq for Shared { impl> Eq for Shared {} impl> Hash for Shared { - fn hash(&self, state: &mut H) where H: Hasher { + fn hash(&self, state: &mut H) + where + H: Hasher, + { self.inner.hash(state) } } impl<'a, T: 'static, MM> ToGlibPtr<'a, *mut T> for Shared -where MM: SharedMemoryManager + 'static { +where + MM: SharedMemoryManager + 'static, +{ type Storage = &'a Self; #[inline] @@ -354,7 +363,9 @@ where MM: SharedMemoryManager + 'static { #[inline] fn to_glib_full(&self) -> *mut T { - unsafe { MM::ref_(self.inner.as_ptr()); } + unsafe { + MM::ref_(self.inner.as_ptr()); + } self.inner.as_ptr() } } diff --git a/src/signal.rs b/src/signal.rs index e7a8159c..4ac28ace 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -48,38 +48,60 @@ impl ToGlib for Inhibit { } } -pub unsafe fn connect_raw(receiver: *mut gobject_sys::GObject, signal_name: *const c_char, trampoline: GCallback, - closure: *mut F) -> SignalHandlerId { +pub unsafe fn connect_raw( + receiver: *mut gobject_sys::GObject, + signal_name: *const c_char, + trampoline: GCallback, + closure: *mut F, +) -> SignalHandlerId { assert_eq!(mem::size_of::<*mut F>(), mem::size_of::()); assert!(trampoline.is_some()); - let handle = gobject_sys::g_signal_connect_data(receiver, signal_name, - trampoline, closure as *mut _, Some(destroy_closure::), 0); + let handle = gobject_sys::g_signal_connect_data( + receiver, + signal_name, + trampoline, + closure as *mut _, + Some(destroy_closure::), + 0, + ); assert!(handle > 0); from_glib(handle) } pub fn signal_handler_block(instance: &T, handler_id: &SignalHandlerId) { unsafe { - gobject_sys::g_signal_handler_block(instance.as_object_ref().to_glib_none().0, handler_id.to_glib()); + gobject_sys::g_signal_handler_block( + instance.as_object_ref().to_glib_none().0, + handler_id.to_glib(), + ); } } pub fn signal_handler_unblock(instance: &T, handler_id: &SignalHandlerId) { unsafe { - gobject_sys::g_signal_handler_unblock(instance.as_object_ref().to_glib_none().0, handler_id.to_glib()); + gobject_sys::g_signal_handler_unblock( + instance.as_object_ref().to_glib_none().0, + handler_id.to_glib(), + ); } } #[allow(clippy::needless_pass_by_value)] pub fn signal_handler_disconnect(instance: &T, handler_id: SignalHandlerId) { unsafe { - gobject_sys::g_signal_handler_disconnect(instance.as_object_ref().to_glib_none().0, handler_id.to_glib()); + gobject_sys::g_signal_handler_disconnect( + instance.as_object_ref().to_glib_none().0, + handler_id.to_glib(), + ); } } pub fn signal_stop_emission_by_name(instance: &T, signal_name: &str) { unsafe { - gobject_sys::g_signal_stop_emission_by_name(instance.as_object_ref().to_glib_none().0, signal_name.to_glib_none().0); + gobject_sys::g_signal_stop_emission_by_name( + instance.as_object_ref().to_glib_none().0, + signal_name.to_glib_none().0, + ); } } diff --git a/src/source.rs b/src/source.rs index adc0e783..ce13805b 100644 --- a/src/source.rs +++ b/src/source.rs @@ -70,7 +70,7 @@ impl ToGlib for Continue { /// Unwinding propagation guard. Aborts the process if destroyed while /// panicking. -#[deprecated(note="Rustc has this functionality built-in since 1.26.0")] +#[deprecated(note = "Rustc has this functionality built-in since 1.26.0")] pub struct CallbackGuard(()); #[allow(deprecated)] @@ -114,7 +114,11 @@ fn into_raw Continue + 'static>(func: F) -> gpointer { Box::into_raw(func) as gpointer } -unsafe extern "C" fn trampoline_child_watch(pid: glib_sys::GPid, status: i32, func: gpointer) { +unsafe extern "C" fn trampoline_child_watch( + pid: glib_sys::GPid, + status: i32, + func: gpointer, +) { let func: &RefCell = &*(func as *const RefCell); (&mut *func.borrow_mut())(Pid(pid), status) } @@ -129,13 +133,19 @@ fn into_raw_child_watch(func: F) -> gpointer { } #[cfg(any(unix, feature = "dox"))] -unsafe extern "C" fn trampoline_unix_fd Continue + 'static>(fd: i32, condition: glib_sys::GIOCondition, func: gpointer) -> gboolean { +unsafe extern "C" fn trampoline_unix_fd Continue + 'static>( + fd: i32, + condition: glib_sys::GIOCondition, + func: gpointer, +) -> gboolean { let func: &RefCell = &*(func as *const RefCell); (&mut *func.borrow_mut())(fd, from_glib(condition)).to_glib() } #[cfg(any(unix, feature = "dox"))] -unsafe extern "C" fn destroy_closure_unix_fd Continue + 'static>(ptr: gpointer) { +unsafe extern "C" fn destroy_closure_unix_fd Continue + 'static>( + ptr: gpointer, +) { Box::>::from_raw(ptr as *mut _); } @@ -152,10 +162,16 @@ fn into_raw_unix_fd Continue + 'static>(func: F) /// The default main loop almost always is the main loop of the main thread. /// Thus the closure is called on the main thread. pub fn idle_add(func: F) -> SourceId -where F: FnMut() -> Continue + Send + 'static { +where + F: FnMut() -> Continue + Send + 'static, +{ unsafe { - from_glib(glib_sys::g_idle_add_full(glib_sys::G_PRIORITY_DEFAULT_IDLE, Some(trampoline::), - into_raw(func), Some(destroy_closure::))) + from_glib(glib_sys::g_idle_add_full( + glib_sys::G_PRIORITY_DEFAULT_IDLE, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + )) } } @@ -172,11 +188,17 @@ where F: FnMut() -> Continue + Send + 'static { /// This function panics if called from a different thread than the one that /// owns the main context. pub fn idle_add_local(func: F) -> SourceId -where F: FnMut() -> Continue + 'static { +where + F: FnMut() -> Continue + 'static, +{ unsafe { assert!(MainContext::default().is_owner()); - from_glib(glib_sys::g_idle_add_full(glib_sys::G_PRIORITY_DEFAULT_IDLE, Some(trampoline::), - into_raw(func), Some(destroy_closure::))) + from_glib(glib_sys::g_idle_add_full( + glib_sys::G_PRIORITY_DEFAULT_IDLE, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + )) } } @@ -191,10 +213,17 @@ where F: FnMut() -> Continue + 'static { /// The default main loop almost always is the main loop of the main thread. /// Thus the closure is called on the main thread. pub fn timeout_add(interval: u32, func: F) -> SourceId -where F: FnMut() -> Continue + Send + 'static { +where + F: FnMut() -> Continue + Send + 'static, +{ unsafe { - from_glib(glib_sys::g_timeout_add_full(glib_sys::G_PRIORITY_DEFAULT, interval, - Some(trampoline::), into_raw(func), Some(destroy_closure::))) + from_glib(glib_sys::g_timeout_add_full( + glib_sys::G_PRIORITY_DEFAULT, + interval, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + )) } } @@ -215,11 +244,18 @@ where F: FnMut() -> Continue + Send + 'static { /// This function panics if called from a different thread than the one that /// owns the main context. pub fn timeout_add_local(interval: u32, func: F) -> SourceId -where F: FnMut() -> Continue + 'static { +where + F: FnMut() -> Continue + 'static, +{ unsafe { assert!(MainContext::default().is_owner()); - from_glib(glib_sys::g_timeout_add_full(glib_sys::G_PRIORITY_DEFAULT, interval, - Some(trampoline::), into_raw(func), Some(destroy_closure::))) + from_glib(glib_sys::g_timeout_add_full( + glib_sys::G_PRIORITY_DEFAULT, + interval, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + )) } } @@ -233,10 +269,17 @@ where F: FnMut() -> Continue + 'static { /// The default main loop almost always is the main loop of the main thread. /// Thus the closure is called on the main thread. pub fn timeout_add_seconds(interval: u32, func: F) -> SourceId -where F: FnMut() -> Continue + Send + 'static { +where + F: FnMut() -> Continue + Send + 'static, +{ unsafe { - from_glib(glib_sys::g_timeout_add_seconds_full(glib_sys::G_PRIORITY_DEFAULT, interval, - Some(trampoline::), into_raw(func), Some(destroy_closure::))) + from_glib(glib_sys::g_timeout_add_seconds_full( + glib_sys::G_PRIORITY_DEFAULT, + interval, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + )) } } @@ -256,11 +299,18 @@ where F: FnMut() -> Continue + Send + 'static { /// This function panics if called from a different thread than the one that /// owns the main context. pub fn timeout_add_seconds_local(interval: u32, func: F) -> SourceId -where F: FnMut() -> Continue + 'static { +where + F: FnMut() -> Continue + 'static, +{ unsafe { assert!(MainContext::default().is_owner()); - from_glib(glib_sys::g_timeout_add_seconds_full(glib_sys::G_PRIORITY_DEFAULT, interval, - Some(trampoline::), into_raw(func), Some(destroy_closure::))) + from_glib(glib_sys::g_timeout_add_seconds_full( + glib_sys::G_PRIORITY_DEFAULT, + interval, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + )) } } @@ -269,10 +319,17 @@ where F: FnMut() -> Continue + 'static { /// /// `func` will be called when `pid` exits pub fn child_watch_add(pid: Pid, func: F) -> SourceId -where F: FnMut(Pid, i32) + Send + 'static { +where + F: FnMut(Pid, i32) + Send + 'static, +{ unsafe { - from_glib(glib_sys::g_child_watch_add_full(glib_sys::G_PRIORITY_DEFAULT, pid.0, - Some(transmute(trampoline_child_watch:: as usize)), into_raw_child_watch(func), Some(destroy_closure_child_watch::))) + from_glib(glib_sys::g_child_watch_add_full( + glib_sys::G_PRIORITY_DEFAULT, + pid.0, + Some(transmute(trampoline_child_watch:: as usize)), + into_raw_child_watch(func), + Some(destroy_closure_child_watch::), + )) } } @@ -287,11 +344,18 @@ where F: FnMut(Pid, i32) + Send + 'static { /// This function panics if called from a different thread than the one that /// owns the main context. pub fn child_watch_add_local(pid: Pid, func: F) -> SourceId -where F: FnMut(Pid, i32) + 'static { +where + F: FnMut(Pid, i32) + 'static, +{ unsafe { assert!(MainContext::default().is_owner()); - from_glib(glib_sys::g_child_watch_add_full(glib_sys::G_PRIORITY_DEFAULT, pid.0, - Some(transmute(trampoline_child_watch:: as usize)), into_raw_child_watch(func), Some(destroy_closure_child_watch::))) + from_glib(glib_sys::g_child_watch_add_full( + glib_sys::G_PRIORITY_DEFAULT, + pid.0, + Some(transmute(trampoline_child_watch:: as usize)), + into_raw_child_watch(func), + Some(destroy_closure_child_watch::), + )) } } @@ -304,10 +368,17 @@ where F: FnMut(Pid, i32) + 'static { /// The default main loop almost always is the main loop of the main thread. /// Thus the closure is called on the main thread. pub fn unix_signal_add(signum: i32, func: F) -> SourceId -where F: FnMut() -> Continue + Send + 'static { +where + F: FnMut() -> Continue + Send + 'static, +{ unsafe { - from_glib(glib_sys::g_unix_signal_add_full(glib_sys::G_PRIORITY_DEFAULT, signum, - Some(trampoline::), into_raw(func), Some(destroy_closure::))) + from_glib(glib_sys::g_unix_signal_add_full( + glib_sys::G_PRIORITY_DEFAULT, + signum, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + )) } } @@ -326,11 +397,18 @@ where F: FnMut() -> Continue + Send + 'static { /// This function panics if called from a different thread than the one that /// owns the main context. pub fn unix_signal_add_local(signum: i32, func: F) -> SourceId -where F: FnMut() -> Continue + 'static { +where + F: FnMut() -> Continue + 'static, +{ unsafe { assert!(MainContext::default().is_owner()); - from_glib(glib_sys::g_unix_signal_add_full(glib_sys::G_PRIORITY_DEFAULT, signum, - Some(trampoline::), into_raw(func), Some(destroy_closure::))) + from_glib(glib_sys::g_unix_signal_add_full( + glib_sys::G_PRIORITY_DEFAULT, + signum, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + )) } } @@ -344,10 +422,18 @@ where F: FnMut() -> Continue + 'static { /// The default main loop almost always is the main loop of the main thread. /// Thus the closure is called on the main thread. pub fn unix_fd_add(fd: RawFd, condition: IOCondition, func: F) -> SourceId -where F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static { +where + F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static, +{ unsafe { - from_glib(glib_sys::g_unix_fd_add_full(glib_sys::G_PRIORITY_DEFAULT, fd, condition.to_glib(), - Some(transmute(trampoline_unix_fd:: as usize)), into_raw_unix_fd(func), Some(destroy_closure_unix_fd::))) + from_glib(glib_sys::g_unix_fd_add_full( + glib_sys::G_PRIORITY_DEFAULT, + fd, + condition.to_glib(), + Some(transmute(trampoline_unix_fd:: as usize)), + into_raw_unix_fd(func), + Some(destroy_closure_unix_fd::), + )) } } @@ -367,11 +453,19 @@ where F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static { /// This function panics if called from a different thread than the one that /// owns the main context. pub fn unix_fd_add_local(fd: RawFd, condition: IOCondition, func: F) -> SourceId -where F: FnMut(RawFd, IOCondition) -> Continue + 'static { +where + F: FnMut(RawFd, IOCondition) -> Continue + 'static, +{ unsafe { assert!(MainContext::default().is_owner()); - from_glib(glib_sys::g_unix_fd_add_full(glib_sys::G_PRIORITY_DEFAULT, fd, condition.to_glib(), - Some(transmute(trampoline_unix_fd:: as usize)), into_raw_unix_fd(func), Some(destroy_closure_unix_fd::))) + from_glib(glib_sys::g_unix_fd_add_full( + glib_sys::G_PRIORITY_DEFAULT, + fd, + condition.to_glib(), + Some(transmute(trampoline_unix_fd:: as usize)), + into_raw_unix_fd(func), + Some(destroy_closure_unix_fd::), + )) } } @@ -428,10 +522,17 @@ pub const PRIORITY_LOW: Priority = Priority(glib_sys::G_PRIORITY_LOW); /// /// `func` will be called repeatedly until it returns `Continue(false)`. pub fn idle_source_new(name: Option<&str>, priority: Priority, func: F) -> Source -where F: FnMut() -> Continue + Send + 'static { +where + F: FnMut() -> Continue + Send + 'static, +{ unsafe { let source = glib_sys::g_idle_source_new(); - glib_sys::g_source_set_callback(source, Some(trampoline::), into_raw(func), Some(destroy_closure::)); + glib_sys::g_source_set_callback( + source, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + ); glib_sys::g_source_set_priority(source, priority.to_glib()); if let Some(name) = name { @@ -449,11 +550,23 @@ where F: FnMut() -> Continue + Send + 'static { /// returns `Continue(false)`. Precise timing is not guaranteed, the timeout may /// be delayed by other events. Prefer `timeout_add_seconds` when millisecond /// precision is not necessary. -pub fn timeout_source_new(interval: u32, name: Option<&str>, priority: Priority, func: F) -> Source -where F: FnMut() -> Continue + Send + 'static { +pub fn timeout_source_new( + interval: u32, + name: Option<&str>, + priority: Priority, + func: F, +) -> Source +where + F: FnMut() -> Continue + Send + 'static, +{ unsafe { let source = glib_sys::g_timeout_source_new(interval); - glib_sys::g_source_set_callback(source, Some(trampoline::), into_raw(func), Some(destroy_closure::)); + glib_sys::g_source_set_callback( + source, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + ); glib_sys::g_source_set_priority(source, priority.to_glib()); if let Some(name) = name { @@ -470,11 +583,23 @@ where F: FnMut() -> Continue + Send + 'static { /// `func` will be called repeatedly every `interval` seconds until it /// returns `Continue(false)`. Precise timing is not guaranteed, the timeout may /// be delayed by other events. -pub fn timeout_source_new_seconds(interval: u32, name: Option<&str>, priority: Priority, func: F) -> Source -where F: FnMut() -> Continue + Send + 'static { +pub fn timeout_source_new_seconds( + interval: u32, + name: Option<&str>, + priority: Priority, + func: F, +) -> Source +where + F: FnMut() -> Continue + Send + 'static, +{ unsafe { let source = glib_sys::g_timeout_source_new_seconds(interval); - glib_sys::g_source_set_callback(source, Some(trampoline::), into_raw(func), Some(destroy_closure::)); + glib_sys::g_source_set_callback( + source, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + ); glib_sys::g_source_set_priority(source, priority.to_glib()); if let Some(name) = name { @@ -489,11 +614,23 @@ where F: FnMut() -> Continue + Send + 'static { /// process exits. /// /// `func` will be called when `pid` exits -pub fn child_watch_source_new(pid: Pid, name: Option<&str>, priority: Priority, func: F) -> Source -where F: FnMut(Pid, i32) + Send + 'static { +pub fn child_watch_source_new( + pid: Pid, + name: Option<&str>, + priority: Priority, + func: F, +) -> Source +where + F: FnMut(Pid, i32) + Send + 'static, +{ unsafe { let source = glib_sys::g_child_watch_source_new(pid.0); - glib_sys::g_source_set_callback(source, Some(transmute(trampoline_child_watch:: as usize)), into_raw_child_watch(func), Some(destroy_closure_child_watch::)); + glib_sys::g_source_set_callback( + source, + Some(transmute(trampoline_child_watch:: as usize)), + into_raw_child_watch(func), + Some(destroy_closure_child_watch::), + ); glib_sys::g_source_set_priority(source, priority.to_glib()); if let Some(name) = name { @@ -510,11 +647,23 @@ where F: FnMut(Pid, i32) + Send + 'static { /// /// `func` will be called repeatedly every time `signum` is raised until it /// returns `Continue(false)`. -pub fn unix_signal_source_new(signum: i32, name: Option<&str>, priority: Priority, func: F) -> Source -where F: FnMut() -> Continue + Send + 'static { +pub fn unix_signal_source_new( + signum: i32, + name: Option<&str>, + priority: Priority, + func: F, +) -> Source +where + F: FnMut() -> Continue + Send + 'static, +{ unsafe { let source = glib_sys::g_unix_signal_source_new(signum); - glib_sys::g_source_set_callback(source, Some(trampoline::), into_raw(func), Some(destroy_closure::)); + glib_sys::g_source_set_callback( + source, + Some(trampoline::), + into_raw(func), + Some(destroy_closure::), + ); glib_sys::g_source_set_priority(source, priority.to_glib()); if let Some(name) = name { @@ -531,11 +680,24 @@ where F: FnMut() -> Continue + Send + 'static { /// /// `func` will be called repeatedly while the file descriptor matches the given IO condition /// until it returns `Continue(false)`. -pub fn unix_fd_source_new(fd: RawFd, condition: IOCondition, name: Option<&str>, priority: Priority, func: F) -> Source -where F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static { +pub fn unix_fd_source_new( + fd: RawFd, + condition: IOCondition, + name: Option<&str>, + priority: Priority, + func: F, +) -> Source +where + F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static, +{ unsafe { let source = glib_sys::g_unix_fd_source_new(fd, condition.to_glib()); - glib_sys::g_source_set_callback(source, Some(transmute(trampoline_unix_fd:: as usize)), into_raw_unix_fd(func), Some(destroy_closure_unix_fd::)); + glib_sys::g_source_set_callback( + source, + Some(transmute(trampoline_unix_fd:: as usize)), + into_raw_unix_fd(func), + Some(destroy_closure_unix_fd::), + ); glib_sys::g_source_set_priority(source, priority.to_glib()); if let Some(name) = name { @@ -549,13 +711,19 @@ where F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static { impl Source { pub fn attach(&self, context: Option<&MainContext>) -> SourceId { unsafe { - from_glib(glib_sys::g_source_attach(self.to_glib_none().0, context.to_glib_none().0)) + from_glib(glib_sys::g_source_attach( + self.to_glib_none().0, + context.to_glib_none().0, + )) } } pub fn remove(tag: SourceId) -> Result<(), ::BoolError> { unsafe { - glib_result_from_gboolean!(glib_sys::g_source_remove(tag.to_glib()), "Failed to remove source") + glib_result_from_gboolean!( + glib_sys::g_source_remove(tag.to_glib()), + "Failed to remove source" + ) } } } diff --git a/src/string.rs b/src/string.rs index 246fc1ef..b263559c 100644 --- a/src/string.rs +++ b/src/string.rs @@ -32,47 +32,64 @@ unsafe impl Sync for String {} impl String { pub fn new>(data: T) -> String { let bytes = data.as_ref(); - unsafe { from_glib_full(glib_sys::g_string_new_len( - bytes.as_ptr() as *const _, - bytes.len() as isize)) } + unsafe { + from_glib_full(glib_sys::g_string_new_len( + bytes.as_ptr() as *const _, + bytes.len() as isize, + )) + } } pub fn append(&mut self, val: &str) -> &mut Self { - unsafe { glib_sys::g_string_append_len( - self.to_glib_none_mut().0, - val.to_glib_none().0, - val.len() as isize); } + unsafe { + glib_sys::g_string_append_len( + self.to_glib_none_mut().0, + val.to_glib_none().0, + val.len() as isize, + ); + } self } pub fn insert(&mut self, pos: isize, val: &str) -> &mut Self { - unsafe { glib_sys::g_string_insert_len( - self.to_glib_none_mut().0, - pos, - val.to_glib_none().0, - val.len() as isize); } + unsafe { + glib_sys::g_string_insert_len( + self.to_glib_none_mut().0, + pos, + val.to_glib_none().0, + val.len() as isize, + ); + } self } pub fn overwrite(&mut self, pos: usize, val: &str) -> &mut Self { - unsafe { glib_sys::g_string_overwrite_len( - self.to_glib_none_mut().0, - pos, - val.to_glib_none().0, - val.len() as isize); } + unsafe { + glib_sys::g_string_overwrite_len( + self.to_glib_none_mut().0, + pos, + val.to_glib_none().0, + val.len() as isize, + ); + } self } pub fn prepend(&mut self, val: &str) -> &mut Self { - unsafe { glib_sys::g_string_prepend_len( - self.to_glib_none_mut().0, - val.to_glib_none().0, - val.len() as isize); } + unsafe { + glib_sys::g_string_prepend_len( + self.to_glib_none_mut().0, + val.to_glib_none().0, + val.len() as isize, + ); + } self } pub fn truncate(&mut self, len: usize) -> &mut Self { - unsafe { glib_sys::g_string_truncate(self.to_glib_none_mut().0, len); } + unsafe { + glib_sys::g_string_truncate(self.to_glib_none_mut().0, len); + } self } @@ -110,8 +127,10 @@ impl fmt::Display for String { impl PartialEq for String { fn eq(&self, other: &Self) -> bool { unsafe { - from_glib(glib_sys::g_string_equal(self.to_glib_none().0, - other.to_glib_none().0)) + from_glib(glib_sys::g_string_equal( + self.to_glib_none().0, + other.to_glib_none().0, + )) } } } @@ -131,7 +150,10 @@ impl cmp::Ord for String { } impl hash::Hash for String { - fn hash(&self, state: &mut H) where H: hash::Hasher { + fn hash(&self, state: &mut H) + where + H: hash::Hasher, + { hash::Hash::hash_slice(self.as_ref(), state) } } @@ -201,10 +223,8 @@ mod tests { #[test] fn display() { - let s: ::String = ::String::new("This is a string."); - assert_eq!( - &format!("{}", s), - "This is a string."); + let s: ::String = ::String::new("This is a string."); + assert_eq!(&format!("{}", s), "This is a string."); } #[test] diff --git a/src/subclass/boxed.rs b/src/subclass/boxed.rs index 27c78e48..ddae86df 100644 --- a/src/subclass/boxed.rs +++ b/src/subclass/boxed.rs @@ -46,7 +46,10 @@ pub fn register_boxed_type() -> ::Type { let type_name = CString::new(T::NAME).unwrap(); if gobject_sys::g_type_from_name(type_name.as_ptr()) != gobject_sys::G_TYPE_INVALID { - panic!("Type {} has already been registered", type_name.to_str().unwrap()); + panic!( + "Type {} has already been registered", + type_name.to_str().unwrap() + ); } from_glib(gobject_sys::g_boxed_type_register_static( diff --git a/src/subclass/object.rs b/src/subclass/object.rs index 0ecccc13..3c78d6de 100644 --- a/src/subclass/object.rs +++ b/src/subclass/object.rs @@ -489,7 +489,11 @@ mod test { // Usually this would be implemented on a Rust wrapper type defined // with glib_wrapper!() but for the test the following is susyscient unsafe impl IsImplementable for DummyInterface { - unsafe extern "C" fn interface_init(_iface: glib_sys::gpointer, _iface_data: glib_sys::gpointer) {} + unsafe extern "C" fn interface_init( + _iface: glib_sys::gpointer, + _iface_data: glib_sys::gpointer, + ) { + } } #[test] diff --git a/src/subclass/types.rs b/src/subclass/types.rs index 9a3273c7..8893d4a7 100644 --- a/src/subclass/types.rs +++ b/src/subclass/types.rs @@ -363,8 +363,10 @@ pub trait ObjectSubclass: ObjectImpl + Sized + 'static { } } -unsafe extern "C" fn class_init(klass: glib_sys::gpointer, _klass_data: glib_sys::gpointer) -where +unsafe extern "C" fn class_init( + klass: glib_sys::gpointer, + _klass_data: glib_sys::gpointer, +) where <::ParentType as ObjectType>::RustClassType: IsSubclassable, { let mut data = T::type_data(); @@ -389,8 +391,9 @@ where // class initialization function. { let klass = &mut *(klass as *mut T::Class); - let parent_class = gobject_sys::g_type_class_peek_parent(klass as *mut _ as glib_sys::gpointer) - as *mut ::GlibClassType; + let parent_class = + gobject_sys::g_type_class_peek_parent(klass as *mut _ as glib_sys::gpointer) + as *mut ::GlibClassType; assert!(!parent_class.is_null()); (*data.as_mut()).parent_class = parent_class as glib_sys::gpointer; @@ -470,7 +473,10 @@ where let type_name = CString::new(T::NAME).unwrap(); if gobject_sys::g_type_from_name(type_name.as_ptr()) != gobject_sys::G_TYPE_INVALID { - panic!("Type {} has already been registered", type_name.to_str().unwrap()); + panic!( + "Type {} has already been registered", + type_name.to_str().unwrap() + ); } let type_ = from_glib(gobject_sys::g_type_register_static( diff --git a/src/translate.rs b/src/translate.rs index 69d97722..de619a81 100644 --- a/src/translate.rs +++ b/src/translate.rs @@ -69,24 +69,36 @@ pub trait Ptr: Copy + 'static { impl Ptr for *const T { #[inline] - fn is_null(&self) -> bool { (*self).is_null() } + fn is_null(&self) -> bool { + (*self).is_null() + } #[inline] - fn from(ptr: *mut X) -> *const T { ptr as *const T } + fn from(ptr: *mut X) -> *const T { + ptr as *const T + } #[inline] - fn to(self) -> *mut X { self as *mut X } + fn to(self) -> *mut X { + self as *mut X + } } impl Ptr for *mut T { #[inline] - fn is_null(&self) -> bool { (*self).is_null() } + fn is_null(&self) -> bool { + (*self).is_null() + } #[inline] - fn from(ptr: *mut X) -> *mut T { ptr as *mut T } + fn from(ptr: *mut X) -> *mut T { + ptr as *mut T + } #[inline] - fn to(self) -> *mut X { self as *mut X } + fn to(self) -> *mut X { + self as *mut X + } } /// Overrides pointer mutability. @@ -139,8 +151,7 @@ impl ToBool for glib_sys::gboolean { pub fn some_if T>(cond: B, f: F) -> Option { if cond.to_bool() { Some(f()) - } - else { + } else { None } } @@ -173,10 +184,14 @@ pub fn some_if T>(cond: B, f: F) -> Option { /// } /// } /// ``` -pub struct Stash<'a, P: Copy, T: ?Sized + ToGlibPtr<'a, P>> (pub P, pub >::Storage); +pub struct Stash<'a, P: Copy, T: ?Sized + ToGlibPtr<'a, P>>( + pub P, + pub >::Storage, +); -pub struct StashMut<'a, P: Copy, T: ?Sized> (pub P, pub >::Storage) - where T: ToGlibPtrMut<'a, P>; +pub struct StashMut<'a, P: Copy, T: ?Sized>(pub P, pub >::Storage) +where + T: ToGlibPtrMut<'a, P>; /// Translate a simple type. pub trait ToGlib { @@ -190,7 +205,11 @@ impl ToGlib for bool { #[inline] fn to_glib(&self) -> glib_sys::gboolean { - if *self { glib_sys::GTRUE } else { glib_sys::GFALSE } + if *self { + glib_sys::GTRUE + } else { + glib_sys::GFALSE + } } } @@ -276,27 +295,30 @@ impl<'a, P: Ptr, T: ToGlibPtr<'a, P>> ToGlibPtr<'a, P> for Option { #[inline] fn to_glib_none(&'a self) -> Stash<'a, P, Option> { - self.as_ref().map_or(Stash(Ptr::from::<()>(ptr::null_mut()), None), |s| { - let s = s.to_glib_none(); - Stash(s.0, Some(s.1)) - }) + self.as_ref() + .map_or(Stash(Ptr::from::<()>(ptr::null_mut()), None), |s| { + let s = s.to_glib_none(); + Stash(s.0, Some(s.1)) + }) } #[inline] fn to_glib_full(&self) -> P { - self.as_ref().map_or(Ptr::from::<()>(ptr::null_mut()), ToGlibPtr::to_glib_full) + self.as_ref() + .map_or(Ptr::from::<()>(ptr::null_mut()), ToGlibPtr::to_glib_full) } } -impl <'a, 'opt: 'a, P: Ptr, T: ToGlibPtrMut<'a, P>> ToGlibPtrMut<'a, P> for Option<&'opt mut T> { +impl<'a, 'opt: 'a, P: Ptr, T: ToGlibPtrMut<'a, P>> ToGlibPtrMut<'a, P> for Option<&'opt mut T> { type Storage = Option<>::Storage>; #[inline] fn to_glib_none_mut(&'a mut self) -> StashMut<'a, P, Option<&'opt mut T>> { - self.as_mut().map_or(StashMut(Ptr::from::<()>(ptr::null_mut()), None), |s| { - let s = s.to_glib_none_mut(); - StashMut(s.0, Some(s.1)) - }) + self.as_mut() + .map_or(StashMut(Ptr::from::<()>(ptr::null_mut()), None), |s| { + let s = s.to_glib_none_mut(); + StashMut(s.0, Some(s.1)) + }) } } @@ -320,7 +342,8 @@ impl<'a> ToGlibPtr<'a, *const c_char> for str { #[inline] fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> { - let tmp = CString::new(self).expect("str::ToGlibPtr<*const c_char>: unexpected '\0' character"); + let tmp = + CString::new(self).expect("str::ToGlibPtr<*const c_char>: unexpected '\0' character"); Stash(tmp.as_ptr(), tmp) } @@ -338,24 +361,24 @@ impl<'a> ToGlibPtr<'a, *mut c_char> for str { #[inline] fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> { - let tmp = CString::new(self).expect("str::ToGlibPtr<*mut c_char>: unexpected '\0' character"); + let tmp = + CString::new(self).expect("str::ToGlibPtr<*mut c_char>: unexpected '\0' character"); Stash(tmp.as_ptr() as *mut c_char, tmp) } #[inline] fn to_glib_full(&self) -> *mut c_char { - unsafe { - glib_sys::g_strndup(self.as_ptr() as *mut c_char, self.len() as size_t) - } + unsafe { glib_sys::g_strndup(self.as_ptr() as *mut c_char, self.len() as size_t) } } } -impl <'a> ToGlibPtr<'a, *const c_char> for String { +impl<'a> ToGlibPtr<'a, *const c_char> for String { type Storage = CString; #[inline] fn to_glib_none(&self) -> Stash<'a, *const c_char, String> { - let tmp = CString::new(&self[..]).expect("String::ToGlibPtr<*const c_char>: unexpected '\0' character"); + let tmp = CString::new(&self[..]) + .expect("String::ToGlibPtr<*const c_char>: unexpected '\0' character"); Stash(tmp.as_ptr(), tmp) } @@ -368,20 +391,20 @@ impl <'a> ToGlibPtr<'a, *const c_char> for String { } } -impl <'a> ToGlibPtr<'a, *mut c_char> for String { +impl<'a> ToGlibPtr<'a, *mut c_char> for String { type Storage = CString; #[inline] fn to_glib_none(&self) -> Stash<'a, *mut c_char, String> { - let tmp = CString::new(&self[..]).expect("String::ToGlibPtr<*mut c_char>: unexpected '\0' character"); + let tmp = CString::new(&self[..]) + .expect("String::ToGlibPtr<*mut c_char>: unexpected '\0' character"); Stash(tmp.as_ptr() as *mut c_char, tmp) } #[inline] fn to_glib_full(&self) -> *mut c_char { unsafe { - glib_sys::g_strndup(self.as_ptr() as *const c_char, self.len() as size_t) - as *mut c_char + glib_sys::g_strndup(self.as_ptr() as *const c_char, self.len() as size_t) as *mut c_char } } } @@ -401,8 +424,7 @@ fn path_to_c(path: &Path) -> CString { // Paths on UNIX must not contain NUL bytes, in which case the conversion // to a CString would fail. The only thing we can do then is to panic, as passing // NULL or the empty string to GLib would cause undefined behaviour. - CString::new(path.as_os_str().as_bytes()) - .expect("Invalid path with NUL bytes") + CString::new(path.as_os_str().as_bytes()).expect("Invalid path with NUL bytes") } #[cfg(windows)] @@ -415,7 +437,8 @@ fn path_to_c(path: &Path) -> CString { // representable in UTF-8 and thus can't possibly be passed to GLib. // Passing NULL or the empty string to GLib can lead to undefined behaviour, so // the only safe option seems to be to simply panic here. - let path_str = path.to_str() + let path_str = path + .to_str() .expect("Path can't be represented as UTF-8") .to_owned(); @@ -428,7 +451,8 @@ fn path_to_c(path: &Path) -> CString { CString::new(path_str[4..].as_bytes()) } else { CString::new(path_str.as_bytes()) - }.expect("Invalid path with NUL bytes") + } + .expect("Invalid path with NUL bytes") } #[cfg(not(windows))] @@ -439,8 +463,7 @@ fn os_str_to_c(s: &OsStr) -> CString { // OS string on UNIX must not contain NUL bytes, in which case the conversion // to a CString would fail. The only thing we can do then is to panic, as passing // NULL or the empty string to GLib would cause undefined behaviour. - CString::new(s.as_bytes()) - .expect("Invalid OS String with NUL bytes") + CString::new(s.as_bytes()).expect("Invalid OS String with NUL bytes") } #[cfg(windows)] @@ -453,7 +476,8 @@ fn os_str_to_c(s: &OsStr) -> CString { // representable in UTF-8 and thus can't possibly be passed to GLib. // Passing NULL or the empty string to GLib can lead to undefined behaviour, so // the only safe option seems to be to simply panic here. - let os_str = s.to_str() + let os_str = s + .to_str() .expect("OS String can't be represented as UTF-8") .to_owned(); @@ -557,7 +581,9 @@ impl GlibPtrDefault for OsString { } pub trait ToGlibContainerFromSlice<'a, P> -where Self: Sized { +where + Self: Sized, +{ type Storage; fn to_glib_none_from_slice(t: &'a [Self]) -> (P, Self::Storage); @@ -590,7 +616,7 @@ macro_rules! impl_to_glib_container_from_slice_fundamental { } } } - } + }; } impl_to_glib_container_from_slice_fundamental!(u8); @@ -621,7 +647,8 @@ macro_rules! impl_to_glib_container_from_slice_string { let v: Vec<_> = t.iter().map(ToGlibPtr::to_glib_none).collect(); let v_ptr = unsafe { - let v_ptr = glib_sys::g_malloc0(mem::size_of::<$ffi_name>() * (t.len() + 1)) as *mut $ffi_name; + let v_ptr = glib_sys::g_malloc0(mem::size_of::<$ffi_name>() * (t.len() + 1)) + as *mut $ffi_name; for (i, s) in v.iter().enumerate() { ptr::write(v_ptr.add(i), s.0); @@ -635,7 +662,8 @@ macro_rules! impl_to_glib_container_from_slice_string { fn to_glib_full_from_slice(t: &[$name]) -> *mut $ffi_name { unsafe { - let v_ptr = glib_sys::g_malloc0(mem::size_of::<$ffi_name>() * (t.len() + 1)) as *mut $ffi_name; + let v_ptr = glib_sys::g_malloc0(mem::size_of::<$ffi_name>() * (t.len() + 1)) + as *mut $ffi_name; for (i, s) in t.iter().enumerate() { ptr::write(v_ptr.add(i), s.to_glib_full()); @@ -660,7 +688,8 @@ macro_rules! impl_to_glib_container_from_slice_string { let v: Vec<_> = t.iter().map(ToGlibPtr::to_glib_none).collect(); let v_ptr = unsafe { - let v_ptr = glib_sys::g_malloc0(mem::size_of::<$ffi_name>() * (t.len() + 1)) as *mut $ffi_name; + let v_ptr = glib_sys::g_malloc0(mem::size_of::<$ffi_name>() * (t.len() + 1)) + as *mut $ffi_name; for (i, s) in v.iter().enumerate() { ptr::write(v_ptr.add(i), s.0); @@ -674,7 +703,8 @@ macro_rules! impl_to_glib_container_from_slice_string { fn to_glib_full_from_slice(t: &[$name]) -> *const $ffi_name { unsafe { - let v_ptr = glib_sys::g_malloc0(mem::size_of::<$ffi_name>() * (t.len() + 1)) as *mut $ffi_name; + let v_ptr = glib_sys::g_malloc0(mem::size_of::<$ffi_name>() * (t.len() + 1)) + as *mut $ffi_name; for (i, s) in t.iter().enumerate() { ptr::write(v_ptr.add(i), s.to_glib_full()); @@ -684,7 +714,7 @@ macro_rules! impl_to_glib_container_from_slice_string { } } } - } + }; } impl_to_glib_container_from_slice_string!(&'a str, *mut c_char); @@ -701,13 +731,17 @@ impl_to_glib_container_from_slice_string!(OsString, *mut c_char); impl_to_glib_container_from_slice_string!(OsString, *const c_char); impl<'a, T> ToGlibContainerFromSlice<'a, *mut glib_sys::GList> for T -where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { - type Storage = (Option, Vec::GlibType, T>>); +where + T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType>, +{ + type Storage = ( + Option, + Vec::GlibType, T>>, + ); #[inline] fn to_glib_none_from_slice(t: &'a [T]) -> (*mut glib_sys::GList, Self::Storage) { - let stash_vec: Vec<_> = - t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); + let stash_vec: Vec<_> = t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); let mut list: *mut glib_sys::GList = ptr::null_mut(); unsafe { for stash in &stash_vec { @@ -719,8 +753,7 @@ where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { #[inline] fn to_glib_container_from_slice(t: &'a [T]) -> (*mut glib_sys::GList, Self::Storage) { - let stash_vec: Vec<_> = - t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); + let stash_vec: Vec<_> = t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); let mut list: *mut glib_sys::GList = ptr::null_mut(); unsafe { for stash in &stash_vec { @@ -743,12 +776,18 @@ where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { } impl<'a, T> ToGlibContainerFromSlice<'a, *const glib_sys::GList> for T -where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { - type Storage = (Option, Vec::GlibType, T>>); +where + T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType>, +{ + type Storage = ( + Option, + Vec::GlibType, T>>, + ); #[inline] fn to_glib_none_from_slice(t: &'a [T]) -> (*const glib_sys::GList, Self::Storage) { - let (list, stash) = ToGlibContainerFromSlice::<*mut glib_sys::GList>::to_glib_none_from_slice(t); + let (list, stash) = + ToGlibContainerFromSlice::<*mut glib_sys::GList>::to_glib_none_from_slice(t); (list as *const glib_sys::GList, stash) } @@ -772,13 +811,17 @@ impl Drop for List { } impl<'a, T> ToGlibContainerFromSlice<'a, *mut glib_sys::GSList> for &'a T -where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { - type Storage = (Option, Vec::GlibType, &'a T>>); +where + T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType>, +{ + type Storage = ( + Option, + Vec::GlibType, &'a T>>, + ); #[inline] fn to_glib_none_from_slice(t: &'a [&'a T]) -> (*mut glib_sys::GSList, Self::Storage) { - let stash_vec: Vec<_> = - t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); + let stash_vec: Vec<_> = t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); let mut list: *mut glib_sys::GSList = ptr::null_mut(); unsafe { for stash in &stash_vec { @@ -790,8 +833,7 @@ where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { #[inline] fn to_glib_container_from_slice(t: &'a [&'a T]) -> (*mut glib_sys::GSList, Self::Storage) { - let stash_vec: Vec<_> = - t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); + let stash_vec: Vec<_> = t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); let mut list: *mut glib_sys::GSList = ptr::null_mut(); unsafe { for stash in &stash_vec { @@ -814,12 +856,18 @@ where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { } impl<'a, T> ToGlibContainerFromSlice<'a, *const glib_sys::GSList> for &'a T -where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { - type Storage = (Option, Vec::GlibType, &'a T>>); +where + T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType>, +{ + type Storage = ( + Option, + Vec::GlibType, &'a T>>, + ); #[inline] fn to_glib_none_from_slice(t: &'a [&'a T]) -> (*const glib_sys::GSList, Self::Storage) { - let (list, stash) = ToGlibContainerFromSlice::<*mut glib_sys::GSList>::to_glib_none_from_slice(t); + let (list, stash) = + ToGlibContainerFromSlice::<*mut glib_sys::GSList>::to_glib_none_from_slice(t); (list as *const glib_sys::GSList, stash) } @@ -876,8 +924,12 @@ impl<'a> ToGlibPtr<'a, *mut glib_sys::GHashTable> for HashMap { #[inline] fn to_glib_full(&self) -> *mut glib_sys::GHashTable { unsafe { - let ptr = glib_sys::g_hash_table_new_full(Some(glib_sys::g_str_hash), - Some(glib_sys::g_str_equal), Some(glib_sys::g_free), Some(glib_sys::g_free)); + let ptr = glib_sys::g_hash_table_new_full( + Some(glib_sys::g_str_hash), + Some(glib_sys::g_str_equal), + Some(glib_sys::g_free), + Some(glib_sys::g_free), + ); for (k, v) in self { let k: *mut c_char = k.to_glib_full(); let v: *mut c_char = v.to_glib_full(); @@ -897,12 +949,18 @@ impl Drop for HashTable { } impl<'a, T> ToGlibContainerFromSlice<'a, *const glib_sys::GArray> for &'a T -where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { - type Storage = (Option, Vec::GlibType, &'a T>>); +where + T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType>, +{ + type Storage = ( + Option, + Vec::GlibType, &'a T>>, + ); #[inline] fn to_glib_none_from_slice(t: &'a [&'a T]) -> (*const glib_sys::GArray, Self::Storage) { - let (list, stash) = ToGlibContainerFromSlice::<*mut glib_sys::GArray>::to_glib_none_from_slice(t); + let (list, stash) = + ToGlibContainerFromSlice::<*mut glib_sys::GArray>::to_glib_none_from_slice(t); (list as *const glib_sys::GArray, stash) } @@ -921,18 +979,24 @@ pub struct Array(*mut glib_sys::GArray); impl Drop for Array { fn drop(&mut self) { - unsafe { glib_sys::g_array_free(self.0, false.to_glib()); } + unsafe { + glib_sys::g_array_free(self.0, false.to_glib()); + } } } impl<'a, T> ToGlibContainerFromSlice<'a, *mut glib_sys::GArray> for T -where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { - type Storage = (Option, Vec::GlibType, T>>); +where + T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType>, +{ + type Storage = ( + Option, + Vec::GlibType, T>>, + ); #[inline] fn to_glib_none_from_slice(t: &'a [T]) -> (*mut glib_sys::GArray, Self::Storage) { - let stash_vec: Vec<_> = - t.iter().map(ToGlibPtr::to_glib_none).collect(); + let stash_vec: Vec<_> = t.iter().map(ToGlibPtr::to_glib_none).collect(); let mut arr: *mut glib_sys::GArray = ptr::null_mut(); unsafe { for stash in &stash_vec { @@ -944,8 +1008,7 @@ where T: GlibPtrDefault + ToGlibPtr<'a, ::GlibType> { #[inline] fn to_glib_container_from_slice(t: &'a [T]) -> (*mut glib_sys::GArray, Self::Storage) { - let stash_vec: Vec<_> = - t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); + let stash_vec: Vec<_> = t.iter().rev().map(ToGlibPtr::to_glib_none).collect(); let mut arr: *mut glib_sys::GArray = ptr::null_mut(); unsafe { for stash in &stash_vec { @@ -1020,8 +1083,7 @@ impl FromGlib for Option { fn from_glib(val: i32) -> Option { if val >= 0 { Some(val as u32) - } - else { + } else { None } } @@ -1032,8 +1094,7 @@ impl FromGlib for Option { fn from_glib(val: i64) -> Option { if val >= 0 { Some(val as u64) - } - else { + } else { None } } @@ -1084,24 +1145,33 @@ pub unsafe fn from_glib_borrow>(ptr: P) -> T { impl> FromGlibPtrNone

for Option { #[inline] unsafe fn from_glib_none(ptr: P) -> Option { - if ptr.is_null() { None } - else { Some(from_glib_none(ptr)) } + if ptr.is_null() { + None + } else { + Some(from_glib_none(ptr)) + } } } impl> FromGlibPtrBorrow

for Option { #[inline] unsafe fn from_glib_borrow(ptr: P) -> Option { - if ptr.is_null() { None } - else { Some(from_glib_borrow(ptr)) } + if ptr.is_null() { + None + } else { + Some(from_glib_borrow(ptr)) + } } } impl> FromGlibPtrFull

for Option { #[inline] unsafe fn from_glib_full(ptr: P) -> Option { - if ptr.is_null() { None } - else { Some(from_glib_full(ptr)) } + if ptr.is_null() { + None + } else { + Some(from_glib_full(ptr)) + } } } @@ -1149,8 +1219,7 @@ unsafe fn c_to_path_buf(ptr: *const c_char) -> PathBuf { // GLib paths on UNIX are always in the local encoding, which can be // UTF-8 or anything else really, but is always a NUL-terminated string // and must not contain any other NUL bytes - OsString::from_vec(CStr::from_ptr(ptr).to_bytes().to_vec()) - .into() + OsString::from_vec(CStr::from_ptr(ptr).to_bytes().to_vec()).into() } #[cfg(windows)] @@ -1301,14 +1370,18 @@ pub unsafe fn c_ptr_array_len(mut ptr: *const P) -> usize { } pub trait FromGlibContainerAsVec -where Self: Sized { +where + Self: Sized, +{ unsafe fn from_glib_none_num_as_vec(ptr: P, num: usize) -> Vec; unsafe fn from_glib_container_num_as_vec(ptr: P, num: usize) -> Vec; unsafe fn from_glib_full_num_as_vec(ptr: P, num: usize) -> Vec; } -pub trait FromGlibPtrArrayContainerAsVec : FromGlibContainerAsVec -where Self: Sized { +pub trait FromGlibPtrArrayContainerAsVec: FromGlibContainerAsVec +where + Self: Sized, +{ unsafe fn from_glib_none_as_vec(ptr: PP) -> Vec; unsafe fn from_glib_container_as_vec(ptr: PP) -> Vec; unsafe fn from_glib_full_as_vec(ptr: PP) -> Vec; @@ -1343,7 +1416,10 @@ impl FromGlibContainerAsVec for bool { FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr as *const _, num) } - unsafe fn from_glib_container_num_as_vec(ptr: *mut glib_sys::gboolean, num: usize) -> Vec { + unsafe fn from_glib_container_num_as_vec( + ptr: *mut glib_sys::gboolean, + num: usize, + ) -> Vec { let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num); glib_sys::g_free(ptr as *mut _); res @@ -1395,7 +1471,7 @@ macro_rules! impl_from_glib_container_as_vec_fundamental { FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, num) } } - } + }; } impl_from_glib_container_as_vec_fundamental!(u8); @@ -1487,7 +1563,7 @@ macro_rules! impl_from_glib_container_as_vec_string { FromGlibContainerAsVec::from_glib_full_num_as_vec(ptr, c_ptr_array_len(ptr)) } } - } + }; } // TODO: Deprecate this @@ -1499,7 +1575,7 @@ impl_from_glib_container_as_vec_string!(PathBuf, *mut c_char); impl_from_glib_container_as_vec_string!(OsString, *const c_char); impl_from_glib_container_as_vec_string!(OsString, *mut c_char); -impl > FromGlibContainer for Vec { +impl> FromGlibContainer for Vec { unsafe fn from_glib_none_num(ptr: PP, num: usize) -> Vec { FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num) } @@ -1513,7 +1589,9 @@ impl > FromGlibContainer for } } -impl > FromGlibPtrContainer for Vec { +impl> FromGlibPtrContainer + for Vec +{ unsafe fn from_glib_none(ptr: PP) -> Vec { FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(ptr) } @@ -1528,10 +1606,14 @@ impl > FromGlibPtrCont } impl FromGlibContainerAsVec<::GlibType, *mut glib_sys::GSList> for T -where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + FromGlibPtrFull<::GlibType> { +where + T: GlibPtrDefault + + FromGlibPtrNone<::GlibType> + + FromGlibPtrFull<::GlibType>, +{ unsafe fn from_glib_none_num_as_vec(mut ptr: *mut glib_sys::GSList, num: usize) -> Vec { if num == 0 || ptr.is_null() { - return Vec::new() + return Vec::new(); } let mut res = Vec::with_capacity(num); for _ in 0..num { @@ -1554,7 +1636,7 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro unsafe fn from_glib_full_num_as_vec(mut ptr: *mut glib_sys::GSList, num: usize) -> Vec { if num == 0 || ptr.is_null() { - return Vec::new() + return Vec::new(); } let orig_ptr = ptr; let mut res = Vec::with_capacity(num); @@ -1571,7 +1653,11 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro } impl FromGlibPtrArrayContainerAsVec<::GlibType, *mut glib_sys::GSList> for T -where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + FromGlibPtrFull<::GlibType> { +where + T: GlibPtrDefault + + FromGlibPtrNone<::GlibType> + + FromGlibPtrFull<::GlibType>, +{ unsafe fn from_glib_none_as_vec(ptr: *mut glib_sys::GSList) -> Vec { let num = glib_sys::g_slist_length(ptr) as usize; FromGlibContainer::from_glib_none_num(ptr, num) @@ -1589,10 +1675,14 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro } impl FromGlibContainerAsVec<::GlibType, *mut glib_sys::GList> for T -where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + FromGlibPtrFull<::GlibType> { +where + T: GlibPtrDefault + + FromGlibPtrNone<::GlibType> + + FromGlibPtrFull<::GlibType>, +{ unsafe fn from_glib_none_num_as_vec(mut ptr: *mut glib_sys::GList, num: usize) -> Vec { if num == 0 || ptr.is_null() { - return Vec::new() + return Vec::new(); } let mut res = Vec::with_capacity(num); for _ in 0..num { @@ -1615,7 +1705,7 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro unsafe fn from_glib_full_num_as_vec(mut ptr: *mut glib_sys::GList, num: usize) -> Vec { if num == 0 || ptr.is_null() { - return Vec::new() + return Vec::new(); } let orig_ptr = ptr; let mut res = Vec::with_capacity(num); @@ -1632,7 +1722,11 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro } impl FromGlibPtrArrayContainerAsVec<::GlibType, *mut glib_sys::GList> for T -where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + FromGlibPtrFull<::GlibType> { +where + T: GlibPtrDefault + + FromGlibPtrNone<::GlibType> + + FromGlibPtrFull<::GlibType>, +{ unsafe fn from_glib_none_as_vec(ptr: *mut glib_sys::GList) -> Vec { let num = glib_sys::g_list_length(ptr) as usize; FromGlibContainer::from_glib_none_num(ptr, num) @@ -1650,7 +1744,11 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro } impl FromGlibContainerAsVec<::GlibType, *const glib_sys::GList> for T -where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + FromGlibPtrFull<::GlibType> { +where + T: GlibPtrDefault + + FromGlibPtrNone<::GlibType> + + FromGlibPtrFull<::GlibType>, +{ unsafe fn from_glib_none_num_as_vec(ptr: *const glib_sys::GList, num: usize) -> Vec { FromGlibContainer::from_glib_none_num(mut_override(ptr), num) } @@ -1666,9 +1764,13 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro } } - -impl FromGlibPtrArrayContainerAsVec<::GlibType, *const glib_sys::GList> for T -where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + FromGlibPtrFull<::GlibType> { +impl FromGlibPtrArrayContainerAsVec<::GlibType, *const glib_sys::GList> + for T +where + T: GlibPtrDefault + + FromGlibPtrNone<::GlibType> + + FromGlibPtrFull<::GlibType>, +{ unsafe fn from_glib_none_as_vec(ptr: *const glib_sys::GList) -> Vec { FromGlibPtrContainer::from_glib_none(mut_override(ptr)) } @@ -1685,7 +1787,11 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro } impl FromGlibContainerAsVec<::GlibType, *const glib_sys::GSList> for T -where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + FromGlibPtrFull<::GlibType> { +where + T: GlibPtrDefault + + FromGlibPtrNone<::GlibType> + + FromGlibPtrFull<::GlibType>, +{ unsafe fn from_glib_none_num_as_vec(ptr: *const glib_sys::GSList, num: usize) -> Vec { FromGlibContainer::from_glib_none_num(mut_override(ptr), num) } @@ -1701,9 +1807,13 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro } } - -impl FromGlibPtrArrayContainerAsVec<::GlibType, *const glib_sys::GSList> for T -where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + FromGlibPtrFull<::GlibType> { +impl FromGlibPtrArrayContainerAsVec<::GlibType, *const glib_sys::GSList> + for T +where + T: GlibPtrDefault + + FromGlibPtrNone<::GlibType> + + FromGlibPtrFull<::GlibType>, +{ unsafe fn from_glib_none_as_vec(ptr: *const glib_sys::GSList) -> Vec { FromGlibPtrContainer::from_glib_none(mut_override(ptr)) } @@ -1719,8 +1829,11 @@ where T: GlibPtrDefault + FromGlibPtrNone<::GlibType> + Fro } } -unsafe extern "C" fn read_string_hash_table(key: glib_sys::gpointer, value: glib_sys::gpointer, - hash_map: glib_sys::gpointer) { +unsafe extern "C" fn read_string_hash_table( + key: glib_sys::gpointer, + value: glib_sys::gpointer, + hash_map: glib_sys::gpointer, +) { let key: String = from_glib_none(key as *const c_char); let value: String = from_glib_none(value as *const c_char); let hash_map: &mut HashMap = &mut *(hash_map as *mut HashMap); @@ -1746,8 +1859,11 @@ impl FromGlibContainer<*const c_char, *mut glib_sys::GHashTable> for HashMap for HashMap { unsafe fn from_glib_none(ptr: *mut glib_sys::GHashTable) -> Self { let mut map = HashMap::new(); - glib_sys::g_hash_table_foreach(ptr, Some(read_string_hash_table), - &mut map as *mut HashMap as *mut _); + glib_sys::g_hash_table_foreach( + ptr, + Some(read_string_hash_table), + &mut map as *mut HashMap as *mut _, + ); map } @@ -1768,10 +1884,10 @@ mod tests { use self::tempfile::tempdir; use std::fs; - use std::collections::HashMap; - use glib_sys; use super::*; + use glib_sys; use gstring::GString; + use std::collections::HashMap; #[test] fn string_hash_map() { @@ -1818,19 +1934,46 @@ mod tests { let dir_1 = tmp_dir.path().join("abcd"); fs::create_dir(&dir_1).unwrap(); assert_eq!(::functions::path_get_basename(&dir_1), Some("abcd".into())); - assert_eq!(::functions::path_get_basename(dir_1.canonicalize().unwrap()), Some("abcd".into())); - assert_eq!(::functions::path_get_dirname(dir_1.canonicalize().unwrap()), Some(tmp_dir.path().into())); - assert!(::functions::file_test(&dir_1, ::FileTest::EXISTS | ::FileTest::IS_DIR)); - assert!(::functions::file_test(&dir_1.canonicalize().unwrap(), ::FileTest::EXISTS | ::FileTest::IS_DIR)); + assert_eq!( + ::functions::path_get_basename(dir_1.canonicalize().unwrap()), + Some("abcd".into()) + ); + assert_eq!( + ::functions::path_get_dirname(dir_1.canonicalize().unwrap()), + Some(tmp_dir.path().into()) + ); + assert!(::functions::file_test( + &dir_1, + ::FileTest::EXISTS | ::FileTest::IS_DIR + )); + assert!(::functions::file_test( + &dir_1.canonicalize().unwrap(), + ::FileTest::EXISTS | ::FileTest::IS_DIR + )); // And test with some non-ASCII characters let dir_2 = tmp_dir.as_ref().join("øäöü"); fs::create_dir(&dir_2).unwrap(); - assert_eq!(::functions::path_get_basename(&dir_2), Some("øäöü".into())); - assert_eq!(::functions::path_get_basename(dir_2.canonicalize().unwrap()), Some("øäöü".into())); - assert_eq!(::functions::path_get_dirname(dir_2.canonicalize().unwrap()), Some(tmp_dir.path().into())); - assert!(::functions::file_test(&dir_2, ::FileTest::EXISTS | ::FileTest::IS_DIR)); - assert!(::functions::file_test(&dir_2.canonicalize().unwrap(), ::FileTest::EXISTS | ::FileTest::IS_DIR)); + assert_eq!( + ::functions::path_get_basename(&dir_2), + Some("øäöü".into()) + ); + assert_eq!( + ::functions::path_get_basename(dir_2.canonicalize().unwrap()), + Some("øäöü".into()) + ); + assert_eq!( + ::functions::path_get_dirname(dir_2.canonicalize().unwrap()), + Some(tmp_dir.path().into()) + ); + assert!(::functions::file_test( + &dir_2, + ::FileTest::EXISTS | ::FileTest::IS_DIR + )); + assert!(::functions::file_test( + &dir_2.canonicalize().unwrap(), + ::FileTest::EXISTS | ::FileTest::IS_DIR + )); } #[test] @@ -1844,9 +1987,21 @@ mod tests { let dir_1 = tmp_dir.join("abcd"); fs::create_dir(&dir_1).unwrap(); assert_eq!(::functions::path_get_basename(&dir_1), Some("abcd".into())); - assert_eq!(::functions::path_get_basename(dir_1.canonicalize().unwrap()), Some("abcd".into())); - assert_eq!(::functions::path_get_dirname(dir_1.canonicalize().unwrap()), Some(tmp_dir)); - assert!(::functions::file_test(&dir_1, ::FileTest::EXISTS | ::FileTest::IS_DIR)); - assert!(::functions::file_test(&dir_1.canonicalize().unwrap(), ::FileTest::EXISTS | ::FileTest::IS_DIR)); + assert_eq!( + ::functions::path_get_basename(dir_1.canonicalize().unwrap()), + Some("abcd".into()) + ); + assert_eq!( + ::functions::path_get_dirname(dir_1.canonicalize().unwrap()), + Some(tmp_dir) + ); + assert!(::functions::file_test( + &dir_1, + ::FileTest::EXISTS | ::FileTest::IS_DIR + )); + assert!(::functions::file_test( + &dir_1.canonicalize().unwrap(), + ::FileTest::EXISTS | ::FileTest::IS_DIR + )); } } diff --git a/src/types.rs b/src/types.rs index bc9bb020..c1fbeb7c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -69,21 +69,15 @@ pub enum Type { impl Type { pub fn name(&self) -> String { - unsafe { - from_glib_none(gobject_sys::g_type_name(self.to_glib())) - } + unsafe { from_glib_none(gobject_sys::g_type_name(self.to_glib())) } } pub fn qname(&self) -> ::Quark { - unsafe { - from_glib(gobject_sys::g_type_qname(self.to_glib())) - } + unsafe { from_glib(gobject_sys::g_type_qname(self.to_glib())) } } pub fn is_a(&self, other: &Type) -> bool { - unsafe { - from_glib(gobject_sys::g_type_is_a(self.to_glib(), other.to_glib())) - } + unsafe { from_glib(gobject_sys::g_type_is_a(self.to_glib(), other.to_glib())) } } pub fn parent(&self) -> Option { @@ -117,7 +111,8 @@ impl Type { pub fn interface_prerequisites(&self) -> Vec { unsafe { let mut n_prereqs = 0u32; - let prereqs = gobject_sys::g_type_interface_prerequisites(self.to_glib(), &mut n_prereqs); + let prereqs = + gobject_sys::g_type_interface_prerequisites(self.to_glib(), &mut n_prereqs); FromGlibContainerAsVec::from_glib_full_num_as_vec(prereqs, n_prereqs as usize) } @@ -155,15 +150,15 @@ pub trait StaticType { impl StaticType for Type { fn static_type() -> Type { - unsafe { - from_glib(gobject_sys::g_gtype_get_type()) - } + unsafe { from_glib(gobject_sys::g_gtype_get_type()) } } } impl<'a> FromValueOptional<'a> for Type { unsafe fn from_value_optional(value: &'a Value) -> Option { - Some(from_glib(gobject_sys::g_value_get_gtype(value.to_glib_none().0))) + Some(from_glib(gobject_sys::g_value_get_gtype( + value.to_glib_none().0, + ))) } } @@ -198,7 +193,7 @@ macro_rules! builtin { Type::$val } } - } + }; } builtin!(bool, Bool); @@ -215,25 +210,22 @@ builtin!(String, String); impl<'a> StaticType for [&'a str] { fn static_type() -> Type { - unsafe { - from_glib(glib_sys::g_strv_get_type()) - } + unsafe { from_glib(glib_sys::g_strv_get_type()) } } } impl StaticType for Vec { fn static_type() -> Type { - unsafe { - from_glib(glib_sys::g_strv_get_type()) - } + unsafe { from_glib(glib_sys::g_strv_get_type()) } } } #[inline] pub unsafe fn instance_of(ptr: glib_sys::gconstpointer) -> bool { - from_glib( - gobject_sys::g_type_check_instance_is_a( - ptr as *mut _, ::static_type().to_glib())) + from_glib(gobject_sys::g_type_check_instance_is_a( + ptr as *mut _, + ::static_type().to_glib(), + )) } impl FromGlib for Type { @@ -320,7 +312,8 @@ impl<'a> ToGlibContainerFromSlice<'a, *mut glib_sys::GType> for Type { } unsafe { - let res = glib_sys::g_malloc0(mem::size_of::() * (t.len() + 1)) as *mut glib_sys::GType; + let res = glib_sys::g_malloc0(mem::size_of::() * (t.len() + 1)) + as *mut glib_sys::GType; for (i, v) in t.iter().enumerate() { *res.add(i) = v.to_glib(); } @@ -329,7 +322,6 @@ impl<'a> ToGlibContainerFromSlice<'a, *mut glib_sys::GType> for Type { } } - impl FromGlibContainerAsVec for Type { unsafe fn from_glib_none_num_as_vec(ptr: *const glib_sys::GType, num: usize) -> Vec { if num == 0 || ptr.is_null() { diff --git a/src/utils.rs b/src/utils.rs index f311ddb7..175fa784 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,9 +20,7 @@ pub fn get_program_name() -> Option { } pub fn get_prgname() -> Option { - unsafe { - from_glib_none(glib_sys::g_get_prgname()) - } + unsafe { from_glib_none(glib_sys::g_get_prgname()) } } /// Same as [`set_prgname()`]. @@ -33,27 +31,27 @@ pub fn set_program_name(name: Option<&str>) { } pub fn set_prgname(name: Option<&str>) { - unsafe { - glib_sys::g_set_prgname(name.to_glib_none().0) - } + unsafe { glib_sys::g_set_prgname(name.to_glib_none().0) } } pub fn getenv>(variable_name: K) -> Option { - #[cfg(windows)] - use glib_sys::g_getenv_utf8 as g_getenv; #[cfg(not(windows))] use glib_sys::g_getenv; + #[cfg(windows)] + use glib_sys::g_getenv_utf8 as g_getenv; - unsafe { - from_glib_none(g_getenv(variable_name.as_ref().to_glib_none().0)) - } + unsafe { from_glib_none(g_getenv(variable_name.as_ref().to_glib_none().0)) } } -pub fn setenv, V: AsRef>(variable_name: K, value: V, overwrite: bool) -> Result<(), BoolError> { - #[cfg(windows)] - use glib_sys::g_setenv_utf8 as g_setenv; +pub fn setenv, V: AsRef>( + variable_name: K, + value: V, + overwrite: bool, +) -> Result<(), BoolError> { #[cfg(not(windows))] use glib_sys::g_setenv; + #[cfg(windows)] + use glib_sys::g_setenv_utf8 as g_setenv; unsafe { glib_result_from_gboolean!( @@ -68,125 +66,123 @@ pub fn setenv, V: AsRef>(variable_name: K, value: V, over } pub fn unsetenv>(variable_name: K) { - #[cfg(windows)] - use glib_sys::g_unsetenv_utf8 as g_unsetenv; #[cfg(not(windows))] use glib_sys::g_unsetenv; + #[cfg(windows)] + use glib_sys::g_unsetenv_utf8 as g_unsetenv; - unsafe { - g_unsetenv(variable_name.as_ref().to_glib_none().0) - } + unsafe { g_unsetenv(variable_name.as_ref().to_glib_none().0) } } pub fn environ_getenv>(envp: &[OsString], variable: K) -> Option { unsafe { - from_glib_none(glib_sys::g_environ_getenv(envp.to_glib_none().0, variable.as_ref().to_glib_none().0)) + from_glib_none(glib_sys::g_environ_getenv( + envp.to_glib_none().0, + variable.as_ref().to_glib_none().0, + )) } } pub fn get_user_name() -> Option { - #[cfg(all(windows,target_arch="x86"))] - use glib_sys::g_get_user_name_utf8 as g_get_user_name; - #[cfg(not(all(windows,target_arch="x86")))] + #[cfg(not(all(windows, target_arch = "x86")))] use glib_sys::g_get_user_name; + #[cfg(all(windows, target_arch = "x86"))] + use glib_sys::g_get_user_name_utf8 as g_get_user_name; - unsafe { - from_glib_none(g_get_user_name()) - } + unsafe { from_glib_none(g_get_user_name()) } } pub fn get_real_name() -> Option { - #[cfg(all(windows,target_arch="x86"))] - use glib_sys::g_get_real_name_utf8 as g_get_real_name; - #[cfg(not(all(windows,target_arch="x86")))] + #[cfg(not(all(windows, target_arch = "x86")))] use glib_sys::g_get_real_name; + #[cfg(all(windows, target_arch = "x86"))] + use glib_sys::g_get_real_name_utf8 as g_get_real_name; - unsafe { - from_glib_none(g_get_real_name()) - } + unsafe { from_glib_none(g_get_real_name()) } } pub fn get_current_dir() -> Option { - #[cfg(windows)] - use glib_sys::g_get_current_dir_utf8 as g_get_current_dir; #[cfg(not(windows))] use glib_sys::g_get_current_dir; + #[cfg(windows)] + use glib_sys::g_get_current_dir_utf8 as g_get_current_dir; - unsafe { - from_glib_full(g_get_current_dir()) - } + unsafe { from_glib_full(g_get_current_dir()) } } -pub fn filename_to_uri>(filename: P, hostname: Option<&str>) -> Result { - #[cfg(windows)] - use glib_sys::g_filename_to_uri_utf8 as g_filename_to_uri; +pub fn filename_to_uri>( + filename: P, + hostname: Option<&str>, +) -> Result { #[cfg(not(windows))] use glib_sys::g_filename_to_uri; + #[cfg(windows)] + use glib_sys::g_filename_to_uri_utf8 as g_filename_to_uri; let hostname = hostname.to_glib_none(); unsafe { let mut error = std::ptr::null_mut(); let ret = g_filename_to_uri(filename.as_ref().to_glib_none().0, hostname.0, &mut error); - if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) } + if error.is_null() { + Ok(from_glib_full(ret)) + } else { + Err(from_glib_full(error)) + } } } pub fn filename_from_uri(uri: &str) -> Result<(std::path::PathBuf, Option), Error> { - #[cfg(windows)] - use glib_sys::g_filename_from_uri_utf8 as g_filename_from_uri; #[cfg(not(windows))] use glib_sys::g_filename_from_uri; + #[cfg(windows)] + use glib_sys::g_filename_from_uri_utf8 as g_filename_from_uri; unsafe { let mut hostname = ptr::null_mut(); let mut error = ptr::null_mut(); let ret = g_filename_from_uri(uri.to_glib_none().0, &mut hostname, &mut error); - if error.is_null() { Ok((from_glib_full(ret), from_glib_full(hostname))) } else { Err(from_glib_full(error)) } + if error.is_null() { + Ok((from_glib_full(ret), from_glib_full(hostname))) + } else { + Err(from_glib_full(error)) + } } } pub fn find_program_in_path>(program: P) -> Option { - #[cfg(all(windows,target_arch="x86"))] - use glib_sys::g_find_program_in_path_utf8 as g_find_program_in_path; - #[cfg(not(all(windows,target_arch="x86")))] + #[cfg(not(all(windows, target_arch = "x86")))] use glib_sys::g_find_program_in_path; + #[cfg(all(windows, target_arch = "x86"))] + use glib_sys::g_find_program_in_path_utf8 as g_find_program_in_path; - unsafe { - from_glib_full(g_find_program_in_path(program.as_ref().to_glib_none().0)) - } + unsafe { from_glib_full(g_find_program_in_path(program.as_ref().to_glib_none().0)) } } pub fn get_home_dir() -> Option { - #[cfg(all(windows,target_arch="x86"))] - use glib_sys::g_get_home_dir_utf8 as g_get_home_dir; - #[cfg(not(all(windows,target_arch="x86")))] + #[cfg(not(all(windows, target_arch = "x86")))] use glib_sys::g_get_home_dir; + #[cfg(all(windows, target_arch = "x86"))] + use glib_sys::g_get_home_dir_utf8 as g_get_home_dir; - unsafe { - from_glib_none(g_get_home_dir()) - } + unsafe { from_glib_none(g_get_home_dir()) } } pub fn get_tmp_dir() -> Option { - #[cfg(all(windows,target_arch="x86"))] - use glib_sys::g_get_tmp_dir_utf8 as g_get_tmp_dir; - #[cfg(not(all(windows,target_arch="x86")))] + #[cfg(not(all(windows, target_arch = "x86")))] use glib_sys::g_get_tmp_dir; + #[cfg(all(windows, target_arch = "x86"))] + use glib_sys::g_get_tmp_dir_utf8 as g_get_tmp_dir; - unsafe { - from_glib_none(g_get_tmp_dir()) - } + unsafe { from_glib_none(g_get_tmp_dir()) } } pub fn mkstemp>(tmpl: P) -> i32 { - #[cfg(windows)] - use glib_sys::g_mkstemp_utf8 as g_mkstemp; #[cfg(not(windows))] use glib_sys::g_mkstemp; + #[cfg(windows)] + use glib_sys::g_mkstemp_utf8 as g_mkstemp; - unsafe { - g_mkstemp(tmpl.as_ref().to_glib_none().0) - } + unsafe { g_mkstemp(tmpl.as_ref().to_glib_none().0) } } #[cfg(test)] @@ -195,7 +191,9 @@ mod tests { use std::sync::Mutex; //Mutex to prevent run environment tests parallel - lazy_static! { static ref LOCK: Mutex<()> = Mutex::new(()); } + lazy_static! { + static ref LOCK: Mutex<()> = Mutex::new(()); + } const VAR_NAME: &str = "function_environment_test"; @@ -248,6 +246,5 @@ mod tests { } else { unreachable!(); } - } } diff --git a/src/value.rs b/src/value.rs index d6aa968c..e02cd82a 100644 --- a/src/value.rs +++ b/src/value.rs @@ -108,7 +108,10 @@ impl Value { /// Creates a new `Value` that is initialized with `type_` pub fn from_type(type_: Type) -> Self { unsafe { - assert_eq!(gobject_sys::g_type_check_is_value_type(type_.to_glib()), glib_sys::GTRUE); + assert_eq!( + gobject_sys::g_type_check_is_value_type(type_.to_glib()), + glib_sys::GTRUE + ); let mut value = Value::uninitialized(); gobject_sys::g_value_init(value.to_glib_none_mut().0, type_.to_glib()); value @@ -121,13 +124,13 @@ impl Value { /// to `T` and `Err(self)` otherwise. pub fn downcast<'a, T: FromValueOptional<'a> + SetValue>(self) -> Result, Self> { unsafe { - let ok = from_glib( - gobject_sys::g_type_check_value_holds(mut_override(self.to_glib_none().0), - T::static_type().to_glib())); + let ok = from_glib(gobject_sys::g_type_check_value_holds( + mut_override(self.to_glib_none().0), + T::static_type().to_glib(), + )); if ok { Ok(TypedValue(self, PhantomData)) - } - else { + } else { Err(self) } } @@ -139,15 +142,15 @@ impl Value { /// to `T` and `None` otherwise. pub fn downcast_ref<'a, T: FromValueOptional<'a> + SetValue>(&self) -> Option<&TypedValue> { unsafe { - let ok = from_glib( - gobject_sys::g_type_check_value_holds(mut_override(self.to_glib_none().0), - T::static_type().to_glib())); + let ok = from_glib(gobject_sys::g_type_check_value_holds( + mut_override(self.to_glib_none().0), + T::static_type().to_glib(), + )); if ok { // This transmute is safe because Value and TypedValue have the same // representation: the only difference is the zero-sized phantom data Some(&*(self as *const Value as *const TypedValue)) - } - else { + } else { None } } @@ -161,15 +164,15 @@ impl Value { /// typed `None` values. Use `downcast` or `is` for that. pub fn get<'a, T: FromValueOptional<'a>>(&'a self) -> Option { unsafe { - let ok = from_glib( - gobject_sys::g_type_check_value_holds(mut_override(self.to_glib_none().0), - T::static_type().to_glib())); - if ok { - T::from_value_optional(self) - } - else { - None - } + let ok = from_glib(gobject_sys::g_type_check_value_holds( + mut_override(self.to_glib_none().0), + T::static_type().to_glib(), + )); + if ok { + T::from_value_optional(self) + } else { + None + } } } @@ -188,7 +191,10 @@ impl Value { /// Returns whether `Value`s of type `src` can be transformed to type `dst`. pub fn type_transformable(src: Type, dst: Type) -> bool { unsafe { - from_glib(gobject_sys::g_value_type_transformable(src.to_glib(), dst.to_glib())) + from_glib(gobject_sys::g_value_type_transformable( + src.to_glib(), + dst.to_glib(), + )) } } @@ -201,7 +207,9 @@ impl Value { } } - pub fn try_into_send_value<'a, T: Send + FromValueOptional<'a> + SetValue>(self) -> Result { + pub fn try_into_send_value<'a, T: Send + FromValueOptional<'a> + SetValue>( + self, + ) -> Result { self.downcast::().map(TypedValue::into_send_value) } } @@ -229,12 +237,10 @@ impl Drop for Value { impl fmt::Debug for Value { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { unsafe { - let s: GString = from_glib_full( - gobject_sys::g_strdup_value_contents(self.to_glib_none().0)); + let s: GString = + from_glib_full(gobject_sys::g_strdup_value_contents(self.to_glib_none().0)); - f.debug_tuple("Value") - .field(&s) - .finish() + f.debug_tuple("Value").field(&s).finish() } } } @@ -294,9 +300,8 @@ impl<'a> ToGlibPtr<'a, *mut gobject_sys::GValue> for &'a [&'a dyn ToValue] { type Storage = ValueArray; fn to_glib_none(&'a self) -> Stash<'a, *mut gobject_sys::GValue, Self> { - let mut values: Vec = self.iter() - .map(|v| v.to_value().into_raw()) - .collect(); + let mut values: Vec = + self.iter().map(|v| v.to_value().into_raw()).collect(); Stash(values.as_mut_ptr(), ValueArray(values)) } } @@ -309,13 +314,16 @@ impl<'a> ToGlibContainerFromSlice<'a, *mut gobject_sys::GValue> for &'a Value { (t.as_ptr() as *mut gobject_sys::GValue, t) } - fn to_glib_container_from_slice(t: &'a [&'a Value]) -> (*mut gobject_sys::GValue, &'a [&'a Value]) { + fn to_glib_container_from_slice( + t: &'a [&'a Value], + ) -> (*mut gobject_sys::GValue, &'a [&'a Value]) { if t.is_empty() { return (ptr::null_mut(), t); } unsafe { - let res = glib_sys::g_malloc(mem::size_of::() * t.len()) as *mut gobject_sys::GValue; + let res = glib_sys::g_malloc(mem::size_of::() * t.len()) + as *mut gobject_sys::GValue; ptr::copy_nonoverlapping(t.as_ptr() as *const gobject_sys::GValue, res, t.len()); (res, t) } @@ -327,7 +335,8 @@ impl<'a> ToGlibContainerFromSlice<'a, *mut gobject_sys::GValue> for &'a Value { } unsafe { - let res = glib_sys::g_malloc(mem::size_of::() * t.len()) as *mut gobject_sys::GValue; + let res = glib_sys::g_malloc(mem::size_of::() * t.len()) + as *mut gobject_sys::GValue; for (i, v) in t.iter().enumerate() { gobject_sys::g_value_init(res.add(i), v.type_().to_glib()); gobject_sys::g_value_copy(v.to_glib_none().0, res.add(i)); @@ -341,12 +350,17 @@ impl<'a> ToGlibContainerFromSlice<'a, *mut gobject_sys::GValue> for &'a Value { impl<'a> ToGlibContainerFromSlice<'a, *const gobject_sys::GValue> for &'a Value { type Storage = &'a [&'a Value]; - fn to_glib_none_from_slice(t: &'a [&'a Value]) -> (*const gobject_sys::GValue, &'a [&'a Value]) { - let (ptr, storage) = ToGlibContainerFromSlice::<'a, *mut gobject_sys::GValue>::to_glib_none_from_slice(t); + fn to_glib_none_from_slice( + t: &'a [&'a Value], + ) -> (*const gobject_sys::GValue, &'a [&'a Value]) { + let (ptr, storage) = + ToGlibContainerFromSlice::<'a, *mut gobject_sys::GValue>::to_glib_none_from_slice(t); (ptr as *const _, storage) } - fn to_glib_container_from_slice(_: &'a [&'a Value]) -> (*const gobject_sys::GValue, &'a [&'a Value]) { + fn to_glib_container_from_slice( + _: &'a [&'a Value], + ) -> (*const gobject_sys::GValue, &'a [&'a Value]) { unimplemented!() } @@ -380,8 +394,13 @@ macro_rules! from_glib { } } - impl FromGlibContainerAsVec<*mut gobject_sys::GValue, *mut *mut gobject_sys::GValue> for $name { - unsafe fn from_glib_none_num_as_vec(ptr: *mut *mut gobject_sys::GValue, num: usize) -> Vec { + impl FromGlibContainerAsVec<*mut gobject_sys::GValue, *mut *mut gobject_sys::GValue> + for $name + { + unsafe fn from_glib_none_num_as_vec( + ptr: *mut *mut gobject_sys::GValue, + num: usize, + ) -> Vec { if num == 0 || ptr.is_null() { return Vec::new(); } @@ -393,13 +412,19 @@ macro_rules! from_glib { res } - unsafe fn from_glib_container_num_as_vec(ptr: *mut *mut gobject_sys::GValue, num: usize) -> Vec { + unsafe fn from_glib_container_num_as_vec( + ptr: *mut *mut gobject_sys::GValue, + num: usize, + ) -> Vec { let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num); glib_sys::g_free(ptr as *mut _); res } - unsafe fn from_glib_full_num_as_vec(ptr: *mut *mut gobject_sys::GValue, num: usize) -> Vec { + unsafe fn from_glib_full_num_as_vec( + ptr: *mut *mut gobject_sys::GValue, + num: usize, + ) -> Vec { if num == 0 || ptr.is_null() { return Vec::new(); } @@ -413,7 +438,9 @@ macro_rules! from_glib { } } - impl FromGlibPtrArrayContainerAsVec<*mut gobject_sys::GValue, *mut *mut gobject_sys::GValue> for $name { + impl FromGlibPtrArrayContainerAsVec<*mut gobject_sys::GValue, *mut *mut gobject_sys::GValue> + for $name + { unsafe fn from_glib_none_as_vec(ptr: *mut *mut gobject_sys::GValue) -> Vec { FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, c_ptr_array_len(ptr)) } @@ -427,23 +454,39 @@ macro_rules! from_glib { } } - impl FromGlibContainerAsVec<*mut gobject_sys::GValue, *const *mut gobject_sys::GValue> for $name { - unsafe fn from_glib_none_num_as_vec(ptr: *const *mut gobject_sys::GValue, num: usize) -> Vec { + impl FromGlibContainerAsVec<*mut gobject_sys::GValue, *const *mut gobject_sys::GValue> + for $name + { + unsafe fn from_glib_none_num_as_vec( + ptr: *const *mut gobject_sys::GValue, + num: usize, + ) -> Vec { FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr as *mut *mut _, num) } - unsafe fn from_glib_container_num_as_vec(_: *const *mut gobject_sys::GValue, _: usize) -> Vec { + unsafe fn from_glib_container_num_as_vec( + _: *const *mut gobject_sys::GValue, + _: usize, + ) -> Vec { // Can't free a *const unimplemented!() } - unsafe fn from_glib_full_num_as_vec(_: *const *mut gobject_sys::GValue, _: usize) -> Vec { + unsafe fn from_glib_full_num_as_vec( + _: *const *mut gobject_sys::GValue, + _: usize, + ) -> Vec { // Can't free a *const unimplemented!() } } - impl FromGlibPtrArrayContainerAsVec<*mut gobject_sys::GValue, *const *mut gobject_sys::GValue> for $name { + impl + FromGlibPtrArrayContainerAsVec< + *mut gobject_sys::GValue, + *const *mut gobject_sys::GValue, + > for $name + { unsafe fn from_glib_none_as_vec(ptr: *const *mut gobject_sys::GValue) -> Vec { FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(ptr as *mut *mut _) } @@ -458,7 +501,7 @@ macro_rules! from_glib { unimplemented!() } } - } + }; } from_glib!(Value, |v| v); @@ -502,35 +545,45 @@ impl<'a, T: FromValueOptional<'a> + SetValue> TypedValue { /// /// This method is only available for types that don't support a `None` /// value. - pub fn get_some(&'a self) -> T where T: FromValue<'a> { + pub fn get_some(&'a self) -> T + where + T: FromValue<'a>, + { unsafe { T::from_value(self) } } /// Sets the value. /// /// This method is only available for types that support a `None` value. - pub fn set(&mut self, value: Option<&U>) where T: Borrow { + pub fn set(&mut self, value: Option<&U>) + where + T: Borrow, + { unsafe { SetValueOptional::set_value_optional(&mut self.0, value) } } /// Sets the value to `None`. /// /// This method is only available for types that support a `None` value. - pub fn set_none(&mut self) where T: SetValueOptional { + pub fn set_none(&mut self) + where + T: SetValueOptional, + { unsafe { T::set_value_optional(&mut self.0, None) } } /// Sets the value. - pub fn set_some(&mut self, value: &U) where T: Borrow { + pub fn set_some(&mut self, value: &U) + where + T: Borrow, + { unsafe { SetValue::set_value(&mut self.0, value) } } } impl fmt::Debug for TypedValue { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - f.debug_tuple("TypedValue") - .field(&self.0) - .finish() + f.debug_tuple("TypedValue").field(&self.0).finish() } } @@ -658,7 +711,9 @@ impl SendValue { /// /// Returns `Ok(TypedValue)` if the value carries a type corresponding /// to `T` and `Err(self)` otherwise. - pub fn downcast<'a, T: FromValueOptional<'a> + SetValue + Send>(self) -> Result, Self> { + pub fn downcast<'a, T: FromValueOptional<'a> + SetValue + Send>( + self, + ) -> Result, Self> { self.0.downcast().map_err(SendValue) } @@ -668,15 +723,15 @@ impl SendValue { /// to `T` and `None` otherwise. pub fn downcast_ref<'a, T: FromValueOptional<'a> + SetValue>(&self) -> Option<&TypedValue> { unsafe { - let ok = from_glib( - gobject_sys::g_type_check_value_holds(mut_override(self.to_glib_none().0), - T::static_type().to_glib())); + let ok = from_glib(gobject_sys::g_type_check_value_holds( + mut_override(self.to_glib_none().0), + T::static_type().to_glib(), + )); if ok { // This transmute is safe because SendValue and TypedValue have the same // representation: the only difference is the zero-sized phantom data Some(&*(self as *const SendValue as *const TypedValue)) - } - else { + } else { None } } @@ -690,9 +745,7 @@ impl SendValue { impl fmt::Debug for SendValue { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - f.debug_tuple("SendValue") - .field(&self.0) - .finish() + f.debug_tuple("SendValue").field(&self.0).finish() } } @@ -905,7 +958,9 @@ impl SetValueOptional for String { impl<'a> FromValueOptional<'a> for bool { unsafe fn from_value_optional(value: &'a Value) -> Option { - Some(from_glib(gobject_sys::g_value_get_boolean(value.to_glib_none().0))) + Some(from_glib(gobject_sys::g_value_get_boolean( + value.to_glib_none().0, + ))) } } @@ -940,7 +995,7 @@ macro_rules! numeric { gobject_sys::$set(value.to_glib_none_mut().0, *this) } } - } + }; } numeric!(i8, g_value_get_schar, g_value_set_schar); @@ -969,9 +1024,15 @@ mod tests { #[test] fn test_strv() { let v = vec!["123", "456"].to_value(); - assert_eq!(v.get::>(), Some(vec![GString::from("123"), GString::from("456")])); + assert_eq!( + v.get::>(), + Some(vec![GString::from("123"), GString::from("456")]) + ); let v = vec![String::from("123"), String::from("456")].to_value(); - assert_eq!(v.get::>(), Some(vec![GString::from("123"), GString::from("456")])); + assert_eq!( + v.get::>(), + Some(vec![GString::from("123"), GString::from("456")]) + ); } } diff --git a/src/value_array.rs b/src/value_array.rs index bd864e4d..bf957b57 100644 --- a/src/value_array.rs +++ b/src/value_array.rs @@ -23,9 +23,7 @@ glib_wrapper! { impl ValueArray { pub fn new(n_prealloced: u32) -> ValueArray { - unsafe { - from_glib_full(gobject_sys::g_value_array_new(n_prealloced)) - } + unsafe { from_glib_full(gobject_sys::g_value_array_new(n_prealloced)) } } pub fn append(&mut self, value: &Value) { @@ -37,7 +35,10 @@ impl ValueArray { pub fn get_nth(&self, index_: u32) -> Option { unsafe { - from_glib_none(gobject_sys::g_value_array_get_nth(mut_override(self.to_glib_none().0), index_)) + from_glib_none(gobject_sys::g_value_array_get_nth( + mut_override(self.to_glib_none().0), + index_, + )) } } @@ -65,9 +66,14 @@ impl ValueArray { unsafe { let mut func = compare_func; let func_obj: &mut (dyn FnMut(&Value, &Value) -> Ordering) = &mut func; - let func_ptr = &func_obj as *const &mut (dyn FnMut(&Value, &Value) -> Ordering) as glib_sys::gpointer; - - gobject_sys::g_value_array_sort_with_data(self.to_glib_none_mut().0, Some(compare_func_trampoline), func_ptr); + let func_ptr = &func_obj as *const &mut (dyn FnMut(&Value, &Value) -> Ordering) + as glib_sys::gpointer; + + gobject_sys::g_value_array_sort_with_data( + self.to_glib_none_mut().0, + Some(compare_func_trampoline), + func_ptr, + ); } } } @@ -77,7 +83,10 @@ impl ops::Deref for ValueArray { fn deref(&self) -> &[Value] { unsafe { - slice::from_raw_parts((*self.to_glib_none().0).values as *const Value, (*self.to_glib_none().0).n_values as usize) + slice::from_raw_parts( + (*self.to_glib_none().0).values as *const Value, + (*self.to_glib_none().0).n_values as usize, + ) } } } @@ -85,13 +94,19 @@ impl ops::Deref for ValueArray { impl ops::DerefMut for ValueArray { fn deref_mut(&mut self) -> &mut [Value] { unsafe { - slice::from_raw_parts_mut((*self.to_glib_none().0).values as *mut Value, (*self.to_glib_none().0).n_values as usize) + slice::from_raw_parts_mut( + (*self.to_glib_none().0).values as *mut Value, + (*self.to_glib_none().0).n_values as usize, + ) } } } -unsafe extern "C" fn compare_func_trampoline(a: glib_sys::gconstpointer, b: glib_sys::gconstpointer, func: glib_sys::gpointer) -> i32 -{ +unsafe extern "C" fn compare_func_trampoline( + a: glib_sys::gconstpointer, + b: glib_sys::gconstpointer, + func: glib_sys::gpointer, +) -> i32 { let func = func as *mut &mut (dyn FnMut(&Value, &Value) -> Ordering); let a = &*(a as *const Value); diff --git a/src/variant.rs b/src/variant.rs index 382b969d..0aaa3e4a 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -75,21 +75,29 @@ impl StaticType for Variant { #[doc(hidden)] impl<'a> value::FromValueOptional<'a> for Variant { unsafe fn from_value_optional(value: &Value) -> Option { - from_glib_full(gobject_sys::g_value_dup_variant(ToGlibPtr::to_glib_none(value).0)) + from_glib_full(gobject_sys::g_value_dup_variant( + ToGlibPtr::to_glib_none(value).0, + )) } } #[doc(hidden)] impl value::SetValue for Variant { unsafe fn set_value(value: &mut Value, this: &Self) { - gobject_sys::g_value_set_variant(ToGlibPtrMut::to_glib_none_mut(value).0, ToGlibPtr::<*mut glib_sys::GVariant>::to_glib_none(this).0) + gobject_sys::g_value_set_variant( + ToGlibPtrMut::to_glib_none_mut(value).0, + ToGlibPtr::<*mut glib_sys::GVariant>::to_glib_none(this).0, + ) } } #[doc(hidden)] impl value::SetValueOptional for Variant { unsafe fn set_value_optional(value: &mut Value, this: Option<&Self>) { - gobject_sys::g_value_set_variant(ToGlibPtrMut::to_glib_none_mut(value).0, ToGlibPtr::<*mut glib_sys::GVariant>::to_glib_none(&this).0) + gobject_sys::g_value_set_variant( + ToGlibPtrMut::to_glib_none_mut(value).0, + ToGlibPtr::<*mut glib_sys::GVariant>::to_glib_none(&this).0, + ) } } @@ -123,8 +131,10 @@ impl Variant { "s" | "o" | "g" => { let mut len = 0; let ptr = glib_sys::g_variant_get_string(self.to_glib_none().0, &mut len); - let ret = str::from_utf8_unchecked( - slice::from_raw_parts(ptr as *const u8, len as usize)); + let ret = str::from_utf8_unchecked(slice::from_raw_parts( + ptr as *const u8, + len as usize, + )); Some(ret) } _ => None, @@ -133,8 +143,8 @@ impl Variant { } } -unsafe impl Send for Variant { } -unsafe impl Sync for Variant { } +unsafe impl Send for Variant {} +unsafe impl Sync for Variant {} impl fmt::Debug for Variant { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -149,7 +159,10 @@ impl fmt::Debug for Variant { impl fmt::Display for Variant { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let serialized: GString = unsafe { - from_glib_full(glib_sys::g_variant_print(self.to_glib_none().0, false.to_glib())) + from_glib_full(glib_sys::g_variant_print( + self.to_glib_none().0, + false.to_glib(), + )) }; f.write_str(&serialized) } @@ -158,20 +171,22 @@ impl fmt::Display for Variant { impl PartialEq for Variant { fn eq(&self, other: &Self) -> bool { unsafe { - from_glib( - glib_sys::g_variant_equal(self.to_glib_none().0 as *const _, - other.to_glib_none().0 as *const _)) + from_glib(glib_sys::g_variant_equal( + self.to_glib_none().0 as *const _, + other.to_glib_none().0 as *const _, + )) } } } -impl Eq for Variant { } +impl Eq for Variant {} impl PartialOrd for Variant { fn partial_cmp(&self, other: &Self) -> Option { unsafe { - if glib_sys::g_variant_classify(self.to_glib_none().0) != - glib_sys::g_variant_classify(other.to_glib_none().0) { + if glib_sys::g_variant_classify(self.to_glib_none().0) + != glib_sys::g_variant_classify(other.to_glib_none().0) + { return None; } @@ -179,7 +194,10 @@ impl PartialOrd for Variant { return None; } - let res = glib_sys::g_variant_compare(self.to_glib_none().0 as *const _, other.to_glib_none().0 as *const _); + let res = glib_sys::g_variant_compare( + self.to_glib_none().0 as *const _, + other.to_glib_none().0 as *const _, + ); if res < 0 { Some(Ordering::Less) } else if res > 0 { @@ -193,9 +211,7 @@ impl PartialOrd for Variant { impl Hash for Variant { fn hash(&self, state: &mut H) { - unsafe { - state.write_u32(glib_sys::g_variant_hash(self.to_glib_none().0 as *const _)) - } + unsafe { state.write_u32(glib_sys::g_variant_hash(self.to_glib_none().0 as *const _)) } } } @@ -256,7 +272,7 @@ macro_rules! impl_numeric { } } } - } + }; } impl_numeric!(u8, "y", g_variant_new_byte, g_variant_get_byte); @@ -284,7 +300,9 @@ impl FromVariant for bool { fn from_variant(variant: &Variant) -> Option { unsafe { if variant.is::() { - Some(from_glib(glib_sys::g_variant_get_boolean(variant.to_glib_none().0))) + Some(from_glib(glib_sys::g_variant_get_boolean( + variant.to_glib_none().0, + ))) } else { None } @@ -330,21 +348,21 @@ impl From for Variant { #[cfg(test)] mod tests { - use std::collections::HashSet; use super::*; + use std::collections::HashSet; macro_rules! unsigned { ($name:ident, $ty:ident) => { #[test] fn $name() { let mut n = $ty::max_value(); - while n > 0 { + while n > 0 { let v = Variant::from(n); assert_eq!(v.get(), Some(n)); n /= 2; } } - } + }; } macro_rules! signed { @@ -352,7 +370,7 @@ mod tests { #[test] fn $name() { let mut n = $ty::max_value(); - while n > 0 { + while n > 0 { let v = Variant::from(n); assert_eq!(v.get(), Some(n)); let v = Variant::from(-n); @@ -360,7 +378,7 @@ mod tests { n /= 2; } } - } + }; } unsigned!(test_u8, u8); diff --git a/src/variant_type.rs b/src/variant_type.rs index 666faaab..48a28f7a 100644 --- a/src/variant_type.rs +++ b/src/variant_type.rs @@ -39,8 +39,8 @@ impl VariantType { } } -unsafe impl Send for VariantType { } -unsafe impl Sync for VariantType { } +unsafe impl Send for VariantType {} +unsafe impl Sync for VariantType {} impl Drop for VariantType { fn drop(&mut self) { @@ -69,7 +69,8 @@ impl Deref for VariantType { type Target = VariantTy; fn deref(&self) -> &VariantTy { unsafe { - &*(slice::from_raw_parts(self.ptr as *const u8, self.len) as *const [u8] as *const VariantTy) + &*(slice::from_raw_parts(self.ptr as *const u8, self.len) as *const [u8] + as *const VariantTy) } } } @@ -141,8 +142,11 @@ impl VariantTy { let limit = ptr as usize + type_string.len(); let mut end = 0_usize; unsafe { - let ok = from_glib(glib_sys::g_variant_type_string_scan(ptr as *const _, - limit as *const _, &mut end as *mut usize as *mut _)); + let ok = from_glib(glib_sys::g_variant_type_string_scan( + ptr as *const _, + limit as *const _, + &mut end as *mut usize as *mut _, + )); if ok && end == limit { Ok(&*(type_string.as_bytes() as *const [u8] as *const VariantTy)) } else { @@ -176,7 +180,7 @@ impl VariantTy { } } -unsafe impl Sync for VariantTy { } +unsafe impl Sync for VariantTy {} #[doc(hidden)] impl<'a> ToGlibPtr<'a, *const glib_sys::GVariantType> for VariantTy { @@ -220,16 +224,19 @@ impl StaticType for VariantTy { impl SetValue for VariantTy { unsafe fn set_value(value: &mut Value, this: &Self) { - gobject_sys::g_value_set_boxed(value.to_glib_none_mut().0, this.to_glib_none().0 as glib_sys::gpointer) + gobject_sys::g_value_set_boxed( + value.to_glib_none_mut().0, + this.to_glib_none().0 as glib_sys::gpointer, + ) } } impl SetValueOptional for VariantTy { unsafe fn set_value_optional(value: &mut Value, this: Option<&Self>) { use std::ptr; - let p = match this{ + let p = match this { Some(ref t) => t.to_glib_none().0 as glib_sys::gpointer, - None => ptr::null() + None => ptr::null(), }; gobject_sys::g_value_set_boxed(value.to_glib_none_mut().0, p) } @@ -237,7 +244,8 @@ impl SetValueOptional for VariantTy { impl<'a> FromValueOptional<'a> for &'a VariantTy { unsafe fn from_value_optional(value: &'a Value) -> Option { - let cvty = gobject_sys::g_value_get_boxed(value.to_glib_none().0) as *mut glib_sys::GVariantType; + let cvty = + gobject_sys::g_value_get_boxed(value.to_glib_none().0) as *mut glib_sys::GVariantType; if cvty.is_null() { None } else { @@ -254,16 +262,19 @@ impl StaticType for VariantType { impl SetValue for VariantType { unsafe fn set_value(value: &mut Value, this: &Self) { - gobject_sys::g_value_set_boxed(value.to_glib_none_mut().0, this.to_glib_none().0 as glib_sys::gpointer) + gobject_sys::g_value_set_boxed( + value.to_glib_none_mut().0, + this.to_glib_none().0 as glib_sys::gpointer, + ) } } impl SetValueOptional for VariantType { unsafe fn set_value_optional(value: &mut Value, this: Option<&Self>) { use std::ptr; - let p = match this{ + let p = match this { Some(ref t) => t.to_glib_none().0 as glib_sys::gpointer, - None => ptr::null() + None => ptr::null(), }; gobject_sys::g_value_set_boxed(value.to_glib_none_mut().0, p) } @@ -271,7 +282,9 @@ impl SetValueOptional for VariantType { impl<'a> FromValueOptional<'a> for VariantType { unsafe fn from_value_optional(value: &'a Value) -> Option { - Option::::from_glib_none(gobject_sys::g_value_get_boxed(value.to_glib_none().0) as *mut glib_sys::GVariantType) + Option::::from_glib_none( + gobject_sys::g_value_get_boxed(value.to_glib_none().0) as *mut glib_sys::GVariantType, + ) } } @@ -297,7 +310,7 @@ macro_rules! impl_eq { ::eq(self, other) } } - } + }; } impl_eq!(VariantType, VariantTy); @@ -320,7 +333,7 @@ macro_rules! impl_str_eq { self[..].eq(other.to_str()) } } - } + }; } impl_str_eq!(VariantTy, str); @@ -332,17 +345,20 @@ impl_str_eq!(VariantType, str); impl_str_eq!(VariantType, &'a str); impl_str_eq!(VariantType, String); -impl Eq for VariantType { } +impl Eq for VariantType {} #[cfg(test)] mod tests { + use super::*; use glib_sys; use translate::*; - use super::*; use value::ToValue; unsafe fn equal(ptr1: *const T, ptr2: *const U) -> bool { - from_glib(glib_sys::g_variant_type_equal(ptr1 as *const _, ptr2 as *const _)) + from_glib(glib_sys::g_variant_type_equal( + ptr1 as *const _, + ptr2 as *const _, + )) } #[test] @@ -428,7 +444,6 @@ mod tests { assert_eq!(ty3, ty4); assert_eq!(VariantTy::static_type(), VariantTy::static_type()); - } }