-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gtk: Implement convenience traits for StringObject
Sadly we can't have AsRef/Deref implemented as well :( Fixes #1360
- Loading branch information
1 parent
644540e
commit 6078c0a
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
use crate::StringObject; | ||
use glib::{GStr, GString}; | ||
|
||
impl From<StringObject> for String { | ||
#[inline] | ||
fn from(value: StringObject) -> Self { | ||
skip_assert_initialized!(); | ||
Self::from(&value) | ||
} | ||
} | ||
|
||
impl From<StringObject> for GString { | ||
#[inline] | ||
fn from(value: StringObject) -> Self { | ||
skip_assert_initialized!(); | ||
Self::from(&value) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a StringObject> for String { | ||
#[inline] | ||
fn from(value: &'a StringObject) -> Self { | ||
skip_assert_initialized!(); | ||
value.string().to_string() | ||
} | ||
} | ||
|
||
impl<'a> From<&'a StringObject> for GString { | ||
#[inline] | ||
fn from(value: &'a StringObject) -> Self { | ||
skip_assert_initialized!(); | ||
value.string() | ||
} | ||
} | ||
|
||
impl From<String> for StringObject { | ||
#[inline] | ||
fn from(value: String) -> Self { | ||
skip_assert_initialized!(); | ||
Self::new(&value) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a String> for StringObject { | ||
#[inline] | ||
fn from(value: &'a String) -> Self { | ||
skip_assert_initialized!(); | ||
Self::new(value) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a GStr> for StringObject { | ||
#[inline] | ||
fn from(v: &'a GStr) -> Self { | ||
skip_assert_initialized!(); | ||
Self::new(v) | ||
} | ||
} | ||
|
||
impl From<GString> for StringObject { | ||
#[inline] | ||
fn from(value: GString) -> Self { | ||
skip_assert_initialized!(); | ||
Self::new(&value) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a GString> for StringObject { | ||
#[inline] | ||
fn from(value: &'a GString) -> Self { | ||
skip_assert_initialized!(); | ||
Self::new(value) | ||
} | ||
} | ||
|
||
impl From<&str> for StringObject { | ||
#[inline] | ||
fn from(value: &str) -> Self { | ||
skip_assert_initialized!(); | ||
Self::new(value) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::StringObject; | ||
use crate as gtk4; | ||
|
||
#[test] | ||
fn from_into() { | ||
let obj = StringObject::new("some_str"); | ||
assert_eq!(String::from(&obj), obj.string()); | ||
assert_eq!(String::from(obj), "some_str"); | ||
} | ||
} |