Skip to content

Commit a458d7c

Browse files
committed
glib: add ToGlibPtr implementations for more string types
1 parent 475f44e commit a458d7c

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

glib/src/translate.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,43 @@ impl<'a, P: Ptr, T: ?Sized + ToGlibPtr<'a, P>> ToGlibPtr<'a, P> for &'a T {
501501
}
502502
}
503503

504+
#[doc(hidden)]
505+
#[derive(Debug)]
506+
pub enum CowStash<B, O> {
507+
Borrowed(B),
508+
Owned(O),
509+
}
510+
511+
impl<'a, P: Ptr, T> ToGlibPtr<'a, P> for Cow<'a, T>
512+
where
513+
T: ToOwned + ?Sized + ToGlibPtr<'a, P>,
514+
T::Owned: ToGlibPtr<'a, P>,
515+
{
516+
type Storage = CowStash<T::Storage, <T::Owned as ToGlibPtr<'a, P>>::Storage>;
517+
518+
#[inline]
519+
fn to_glib_none(&'a self) -> Stash<'a, P, Self> {
520+
match self {
521+
Cow::Borrowed(v) => {
522+
let s = v.to_glib_none();
523+
Stash(s.0, CowStash::Borrowed(s.1))
524+
}
525+
Cow::Owned(v) => {
526+
let s = v.to_glib_none();
527+
Stash(s.0, CowStash::Owned(s.1))
528+
}
529+
}
530+
}
531+
532+
#[inline]
533+
fn to_glib_full(&self) -> P {
534+
match self {
535+
Cow::Borrowed(v) => v.to_glib_full(),
536+
Cow::Owned(v) => v.to_glib_full(),
537+
}
538+
}
539+
}
540+
504541
impl<'a> ToGlibPtr<'a, *const c_char> for str {
505542
type Storage = Cow<'static, [u8]>;
506543

@@ -580,6 +617,82 @@ impl<'a> ToGlibPtr<'a, *mut c_char> for String {
580617
}
581618
}
582619

620+
impl<'a> ToGlibPtr<'a, *const c_char> for CStr {
621+
type Storage = &'a Self;
622+
623+
#[inline]
624+
fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> {
625+
Stash(self.as_ptr(), self)
626+
}
627+
628+
#[inline]
629+
fn to_glib_full(&self) -> *const c_char {
630+
unsafe {
631+
ffi::g_strndup(
632+
self.as_ptr() as *const c_char,
633+
self.to_bytes().len() as size_t,
634+
) as *const c_char
635+
}
636+
}
637+
}
638+
639+
impl<'a> ToGlibPtr<'a, *mut c_char> for CStr {
640+
type Storage = &'a Self;
641+
642+
#[inline]
643+
fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> {
644+
Stash(self.as_ptr() as *mut c_char, self)
645+
}
646+
647+
#[inline]
648+
fn to_glib_full(&self) -> *mut c_char {
649+
unsafe {
650+
ffi::g_strndup(
651+
self.as_ptr() as *const c_char,
652+
self.to_bytes().len() as size_t,
653+
) as *mut c_char
654+
}
655+
}
656+
}
657+
658+
impl<'a> ToGlibPtr<'a, *const c_char> for CString {
659+
type Storage = &'a Self;
660+
661+
#[inline]
662+
fn to_glib_none(&'a self) -> Stash<'a, *const c_char, Self> {
663+
Stash(self.as_ptr(), self)
664+
}
665+
666+
#[inline]
667+
fn to_glib_full(&self) -> *const c_char {
668+
unsafe {
669+
ffi::g_strndup(
670+
self.as_ptr() as *const c_char,
671+
self.as_bytes().len() as size_t,
672+
) as *const c_char
673+
}
674+
}
675+
}
676+
677+
impl<'a> ToGlibPtr<'a, *mut c_char> for CString {
678+
type Storage = &'a Self;
679+
680+
#[inline]
681+
fn to_glib_none(&'a self) -> Stash<'a, *mut c_char, Self> {
682+
Stash(self.as_ptr() as *mut c_char, self)
683+
}
684+
685+
#[inline]
686+
fn to_glib_full(&self) -> *mut c_char {
687+
unsafe {
688+
ffi::g_strndup(
689+
self.as_ptr() as *const c_char,
690+
self.as_bytes().len() as size_t,
691+
) as *mut c_char
692+
}
693+
}
694+
}
695+
583696
// rustdoc-stripper-ignore-next
584697
/// Translate to a pointer.
585698
pub trait IntoGlibPtr<P: Ptr> {

0 commit comments

Comments
 (0)