Skip to content

Commit

Permalink
gtk: Implement convenience traits for StringObject
Browse files Browse the repository at this point in the history
Sadly we can't have AsRef/Deref implemented as well :(

Fixes #1360
  • Loading branch information
bilelmoussaoui committed Apr 8, 2023
1 parent 644540e commit 6078c0a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

Bilal Elmoussaoui:
- gtk: Implement convenience traits for StringObject

## [0.6.5]

Fabio Valentini:
Expand Down
1 change: 1 addition & 0 deletions gtk4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ mod signal_list_item_factory;
mod snapshot;
mod spin_button;
mod string_list;
mod string_object;
mod text;
mod text_buffer;
mod tree_model;
Expand Down
97 changes: 97 additions & 0 deletions gtk4/src/string_object.rs
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");
}
}

0 comments on commit 6078c0a

Please sign in to comment.