Skip to content

Commit d02489f

Browse files
committed
glib: add ToGlibPtr implementations for more string types
1 parent 1cbddef commit d02489f

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
@@ -502,6 +502,43 @@ impl<'a, P: Ptr, T: ?Sized + ToGlibPtr<'a, P>> ToGlibPtr<'a, P> for &'a T {
502502
}
503503
}
504504

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

@@ -581,6 +618,82 @@ impl<'a> ToGlibPtr<'a, *mut c_char> for String {
581618
}
582619
}
583620

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

0 commit comments

Comments
 (0)