Skip to content

Commit

Permalink
try manual __imp_ dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Sep 17, 2024
1 parent 06abb15 commit beeaff3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 44 deletions.
4 changes: 0 additions & 4 deletions glib/gobject-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

mod build_manual;

#[cfg(not(docsrs))]
use std::process;

Expand All @@ -16,6 +14,4 @@ fn main() {
println!("cargo:warning={s}");
process::exit(1);
}

build_manual::main();
}
33 changes: 0 additions & 33 deletions glib/gobject-sys/build_manual.rs

This file was deleted.

41 changes: 35 additions & 6 deletions glib/gobject-sys/src/manual.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
use glib_sys::GType;

// `g_param_spec_types` extern binding generated by build.rs
include!(concat!(env!("OUT_DIR"), "/param_spec.rs"));
#[cfg(not(target_os = "windows"))]
pub type GParamSpecType = *const GType;

/// # Safety
/// This should be safe as long as the offset added to g_param_spec_types is in bounds.
pub unsafe fn g_param_spec_types_get_type(offset: usize) -> GType {
*g_param_spec_types.add(offset)
#[cfg(target_os = "windows")]
pub type GParamSpecType = usize;

#[repr(C)]
pub struct GParamSpecTypesWrapper(&'static GParamSpecType);

unsafe impl Sync for GParamSpecTypesWrapper {}

impl GParamSpecTypesWrapper {
unsafe fn get_ptr(&self) -> *const GType {
if cfg!(target_os = "windows") {
*(*self.0 as *const *const GType)
} else {
*self.0 as *const GType
}
}

/// # Safety
/// This should be safe as long as the offset added to g_param_spec_types is in bounds.
pub unsafe fn get_type(&self, offset: usize) -> GType {
*(self.get_ptr()).add(offset)
}
}

extern "C" {
#[cfg_attr(target_os = "windows", link_name = "__imp_g_param_spec_types")]
static g_param_spec_types: GParamSpecType;
}

pub static g_param_spec_types_ref: GParamSpecTypesWrapper =
unsafe { GParamSpecTypesWrapper(&g_param_spec_types) };

//#[cfg(all(target_env = "msvc", not(feature = "static")))]
//pub static __imp_g_param_spec_types: &'static GParamSpecTypes = unsafe { &g_param_spec_types };
2 changes: 1 addition & 1 deletion glib/src/param_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ macro_rules! define_param_spec {
#[inline]
fn static_type() -> Type {
unsafe {
from_glib(gobject_ffi::g_param_spec_types_get_type($rust_type_offset))
from_glib(gobject_sys::g_param_spec_types_ref.get_type($rust_type_offset))
}
}
}
Expand Down

0 comments on commit beeaff3

Please sign in to comment.