Skip to content

Commit

Permalink
Merge pull request #858 from sdroege/0.16-backports
Browse files Browse the repository at this point in the history
0.16 backports
  • Loading branch information
sdroege committed Dec 12, 2022
2 parents b0098e2 + 6ded5e3 commit dbd065a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 14 additions & 2 deletions glib/src/gstring.rs
Expand Up @@ -2,7 +2,7 @@

use crate::translate::*;
use crate::types::{StaticType, Type};
use std::borrow::Borrow;
use std::borrow::{Borrow, Cow};
use std::cmp::Ordering;
use std::ffi::{CStr, CString, OsStr};
use std::fmt;
Expand All @@ -16,7 +16,7 @@ use std::slice;
use std::string::String;

// rustdoc-stripper-ignore-next
/// Representaion of a borrowed [`GString`].
/// Representation of a borrowed [`GString`].
///
/// This type is very similar to [`std::ffi::CStr`], but with one added constraint: the string
/// must also be valid UTF-8.
Expand Down Expand Up @@ -740,6 +740,18 @@ impl From<&CStr> for GString {
}
}

impl<'a> From<GString> for Cow<'a, GStr> {
fn from(v: GString) -> Self {
Cow::Owned(v)
}
}

impl<'a> From<&'a GStr> for Cow<'a, GStr> {
fn from(v: &'a GStr) -> Self {
Cow::Borrowed(v)
}
}

#[doc(hidden)]
impl FromGlibPtrFull<*mut u8> for GString {
#[inline]
Expand Down
9 changes: 9 additions & 0 deletions glib/src/translate.rs
Expand Up @@ -579,6 +579,15 @@ pub trait IntoGlibPtr<P: Ptr> {
unsafe fn into_glib_ptr(self) -> P;
}

impl<P: Ptr, T: IntoGlibPtr<P>> IntoGlibPtr<P> for Option<T> {
#[inline]
unsafe fn into_glib_ptr(self) -> P {
self.map_or(Ptr::from::<()>(ptr::null_mut()), |s| {
IntoGlibPtr::into_glib_ptr(s)
})
}
}

impl GlibPtrDefault for str {
type GlibType = *mut c_char;
}
Expand Down

0 comments on commit dbd065a

Please sign in to comment.