Skip to content

Commit

Permalink
fix: pass str parameters as cstring to glib:::ffi
Browse files Browse the repository at this point in the history
Signed-off-by: fbrouille <fbrouille@users.noreply.github.com>
  • Loading branch information
fbrouille committed Oct 26, 2023
1 parent 8cec480 commit ecfe5fe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions glib-macros/tests/dynamic_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ pub mod plugin {
use glib::translate::{FromGlib, IntoGlib};
use glib::ObjectType;
use std::cell::Cell;
use std::ffi::CString;

// impl for a dynamic interface that extends `MyStaticInterface`
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -499,13 +500,12 @@ pub mod plugin {
flags: glib::TypeFlags,
) -> glib::Type {
let type_ = unsafe {
let mut g_type = glib::gobject_ffi::g_type_from_name(
type_name.as_ptr() as *const libc::c_char
);
let type_name = CString::new(type_name).unwrap();
let mut g_type = glib::gobject_ffi::g_type_from_name(type_name.as_ptr());
if g_type == glib::gobject_ffi::G_TYPE_INVALID {
g_type = glib::gobject_ffi::g_type_register_dynamic(
parent_type.into_glib(),
type_name.as_ptr() as *const libc::c_char,
type_name.as_ptr(),
self.obj().upcast_ref::<glib::TypePlugin>().as_ptr(),
flags.into_glib(),
);
Expand Down
6 changes: 4 additions & 2 deletions glib/src/subclass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@
//! use glib::subclass::prelude::*;
//!
//! pub mod imp {
//! use std::ffi::CString;
//! use super::*;
//! use glib::translate::IntoGlib;
//! use glib::translate::FromGlib;
Expand Down Expand Up @@ -358,10 +359,11 @@
//!
//! fn register_dynamic_type(&self, parent_type: glib::Type, type_name: &str, type_info: &glib::TypeInfo, flags: glib::TypeFlags) -> glib::Type {
//! let type_ = unsafe {
//! let mut g_type = glib::gobject_ffi::g_type_from_name(type_name.as_ptr() as *const libc::c_char);
//! let type_name = CString::new(type_name).unwrap();
//! let mut g_type = glib::gobject_ffi::g_type_from_name(type_name.as_ptr());
//! if g_type == glib::gobject_ffi::G_TYPE_INVALID {
//! // register type
//! g_type = glib::gobject_ffi::g_type_register_dynamic(parent_type.into_glib(), type_name.as_ptr() as *const libc::c_char, self.obj().upcast_ref::<glib::TypePlugin>().as_ptr(), flags.into_glib());
//! g_type = glib::gobject_ffi::g_type_register_dynamic(parent_type.into_glib(), type_name.as_ptr(), self.obj().upcast_ref::<glib::TypePlugin>().as_ptr(), flags.into_glib());
//! }
//! glib::Type::from_glib(g_type)
//! };
Expand Down
8 changes: 5 additions & 3 deletions glib/src/subclass/type_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ mod tests {
use super::*;

mod imp {
use std::ffi::CString;

use super::*;
use crate::object::ObjectType;

Expand Down Expand Up @@ -267,13 +269,13 @@ mod tests {
flags: TypeFlags,
) -> Type {
let type_ = unsafe {
let mut g_type =
gobject_ffi::g_type_from_name(type_name.as_ptr() as *const libc::c_char);
let type_name = CString::new(type_name).unwrap();
let mut g_type = gobject_ffi::g_type_from_name(type_name.as_ptr());
if g_type == gobject_ffi::G_TYPE_INVALID {
// register type
g_type = gobject_ffi::g_type_register_dynamic(
parent_type.into_glib(),
type_name.as_ptr() as *const libc::c_char,
type_name.as_ptr(),
self.obj().upcast_ref::<TypePlugin>().as_ptr(),
flags.into_glib(),
);
Expand Down

0 comments on commit ecfe5fe

Please sign in to comment.