Skip to content

Commit

Permalink
Merge pull request #865 from sdroege/stash-phantom-data
Browse files Browse the repository at this point in the history
Various `Stash` / `to_glib_none()` related optimizations and bugfixes
  • Loading branch information
sdroege authored Dec 20, 2022
2 parents a796130 + e9e7a89 commit d983c4e
Show file tree
Hide file tree
Showing 27 changed files with 181 additions and 163 deletions.
6 changes: 4 additions & 2 deletions cairo/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(feature = "use_glib")]
use std::marker::PhantomData;
use std::{ffi::CString, fmt, mem::MaybeUninit, ops, ptr, slice};

#[cfg(feature = "use_glib")]
Expand Down Expand Up @@ -69,11 +71,11 @@ impl IntoGlibPtr<*mut ffi::cairo_t> for Context {

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_t> for &'a Context {
type Storage = &'a Context;
type Storage = PhantomData<&'a Context>;

#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut ffi::cairo_t, &'a Context> {
Stash(self.0.as_ptr(), *self)
Stash(self.0.as_ptr(), PhantomData)
}

#[inline]
Expand Down
6 changes: 4 additions & 2 deletions cairo/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#[cfg(any(feature = "script", feature = "dox"))]
use std::ffi::CString;
#[cfg(feature = "use_glib")]
use std::marker::PhantomData;
#[cfg(any(feature = "script", feature = "dox"))]
use std::path::Path;
use std::{fmt, ptr};
Expand Down Expand Up @@ -305,11 +307,11 @@ impl IntoGlibPtr<*mut ffi::cairo_device_t> for Device {

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_device_t> for Device {
type Storage = &'a Device;
type Storage = PhantomData<&'a Device>;

#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::cairo_device_t, Self> {
Stash(self.to_raw_none(), self)
Stash(self.to_raw_none(), PhantomData)
}

#[inline]
Expand Down
15 changes: 9 additions & 6 deletions cairo/src/matrices.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::fmt;
#[cfg(feature = "use_glib")]
use std::marker::PhantomData;

use crate::{utils::status_to_result, Error};

Expand Down Expand Up @@ -181,24 +183,25 @@ impl Uninitialized for Matrix {
#[cfg(feature = "use_glib")]
#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::cairo_matrix_t> for Matrix {
type Storage = &'a Self;
type Storage = PhantomData<&'a Self>;

#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *const ffi::cairo_matrix_t, Self> {
let ptr: *const Matrix = self;
Stash(ptr as *const ffi::cairo_matrix_t, self)
Stash(
self as *const Matrix as *const ffi::cairo_matrix_t,
PhantomData,
)
}
}

#[cfg(feature = "use_glib")]
#[doc(hidden)]
impl<'a> ToGlibPtrMut<'a, *mut ffi::cairo_matrix_t> for Matrix {
type Storage = &'a mut Self;
type Storage = PhantomData<&'a mut Self>;

#[inline]
fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::cairo_matrix_t, Self> {
let ptr: *mut Matrix = &mut *self;
StashMut(ptr as *mut ffi::cairo_matrix_t, self)
StashMut(self as *mut Matrix as *mut ffi::cairo_matrix_t, PhantomData)
}
}

Expand Down
18 changes: 11 additions & 7 deletions cairo/src/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::fmt;
#[cfg(feature = "use_glib")]
use std::mem;
use std::{marker::PhantomData, mem};

#[cfg(feature = "use_glib")]
use glib::translate::*;
Expand Down Expand Up @@ -70,24 +70,28 @@ impl Uninitialized for Rectangle {
#[cfg(feature = "use_glib")]
#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::cairo_rectangle_t> for Rectangle {
type Storage = &'a Self;
type Storage = PhantomData<&'a Self>;

#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *const ffi::cairo_rectangle_t, Self> {
let ptr: *const Rectangle = self;
Stash(ptr as *const ffi::cairo_rectangle_t, self)
Stash(
self as *const Rectangle as *const ffi::cairo_rectangle_t,
PhantomData,
)
}
}

#[cfg(feature = "use_glib")]
#[doc(hidden)]
impl<'a> ToGlibPtrMut<'a, *mut ffi::cairo_rectangle_t> for Rectangle {
type Storage = &'a mut Self;
type Storage = PhantomData<&'a mut Self>;

#[inline]
fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::cairo_rectangle_t, Self> {
let ptr: *mut Rectangle = &mut *self;
StashMut(ptr as *mut ffi::cairo_rectangle_t, self)
StashMut(
self as *mut Rectangle as *mut ffi::cairo_rectangle_t,
PhantomData,
)
}
}

Expand Down
18 changes: 11 additions & 7 deletions cairo/src/rectangle_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::fmt;
#[cfg(feature = "use_glib")]
use std::mem;
use std::{marker::PhantomData, mem};

#[cfg(feature = "use_glib")]
use glib::translate::*;
Expand Down Expand Up @@ -70,24 +70,28 @@ impl Uninitialized for RectangleInt {
#[cfg(feature = "use_glib")]
#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::cairo_rectangle_int_t> for RectangleInt {
type Storage = &'a Self;
type Storage = PhantomData<&'a Self>;

#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *const ffi::cairo_rectangle_int_t, Self> {
let ptr: *const RectangleInt = self;
Stash(ptr as *const ffi::cairo_rectangle_int_t, self)
Stash(
self as *const RectangleInt as *const ffi::cairo_rectangle_int_t,
PhantomData,
)
}
}

#[cfg(feature = "use_glib")]
#[doc(hidden)]
impl<'a> ToGlibPtrMut<'a, *mut ffi::cairo_rectangle_int_t> for RectangleInt {
type Storage = &'a mut Self;
type Storage = PhantomData<&'a mut Self>;

#[inline]
fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::cairo_rectangle_int_t, Self> {
let ptr: *mut RectangleInt = &mut *self;
StashMut(ptr as *mut ffi::cairo_rectangle_int_t, self)
StashMut(
self as *mut RectangleInt as *mut ffi::cairo_rectangle_int_t,
PhantomData,
)
}
}

Expand Down
12 changes: 7 additions & 5 deletions cairo/src/region.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(feature = "use_glib")]
use std::marker::PhantomData;
use std::{fmt, ptr};

#[cfg(feature = "use_glib")]
Expand All @@ -22,11 +24,11 @@ impl IntoGlibPtr<*mut ffi::cairo_region_t> for Region {
#[cfg(feature = "use_glib")]
#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_region_t> for &'a Region {
type Storage = &'a Region;
type Storage = PhantomData<&'a Region>;

#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut ffi::cairo_region_t, &'a Region> {
Stash(self.0.as_ptr(), *self)
fn to_glib_none(&self) -> Stash<'a, *mut ffi::cairo_region_t, Self> {
Stash(self.0.as_ptr(), PhantomData)
}

#[inline]
Expand All @@ -38,13 +40,13 @@ impl<'a> ToGlibPtr<'a, *mut ffi::cairo_region_t> for &'a Region {
#[cfg(feature = "use_glib")]
#[doc(hidden)]
impl<'a> ToGlibPtrMut<'a, *mut ffi::cairo_region_t> for Region {
type Storage = &'a mut Self;
type Storage = PhantomData<&'a mut Self>;

// FIXME: This is unsafe: regions are reference counted, so we could get multiple mutable
// references here
#[inline]
fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::cairo_region_t, Self> {
StashMut(self.0.as_ptr(), self)
StashMut(self.0.as_ptr(), PhantomData)
}
}

Expand Down
6 changes: 4 additions & 2 deletions cairo/src/surface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(feature = "use_glib")]
use std::marker::PhantomData;
use std::{ffi::CString, fmt, ops::Deref, ptr, slice};

#[cfg(feature = "use_glib")]
Expand Down Expand Up @@ -292,11 +294,11 @@ impl IntoGlibPtr<*mut ffi::cairo_surface_t> for Surface {

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_surface_t> for Surface {
type Storage = &'a Surface;
type Storage = PhantomData<&'a Surface>;

#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::cairo_surface_t, Self> {
Stash(self.to_raw_none(), self)
Stash(self.to_raw_none(), PhantomData)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion cairo/src/surface_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ macro_rules! declare_surface {

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_surface_t> for $surf_name {
type Storage = &'a Surface;
type Storage = std::marker::PhantomData<&'a Surface>;

#[inline]
fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::cairo_surface_t, Self> {
Expand Down
18 changes: 10 additions & 8 deletions cairo/src/xcb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(feature = "use_glib")]
use std::marker::PhantomData;
use std::{convert::TryFrom, fmt, ops::Deref, ptr};

#[cfg(feature = "use_glib")]
Expand Down Expand Up @@ -64,11 +66,11 @@ impl XCBConnection {

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::xcb_connection_t> for &'a XCBConnection {
type Storage = &'a XCBConnection;
type Storage = PhantomData<&'a XCBConnection>;

#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_connection_t, &'a XCBConnection> {
Stash(self.to_raw_none(), *self)
Stash(self.to_raw_none(), PhantomData)
}
}

Expand Down Expand Up @@ -137,13 +139,13 @@ impl XCBRenderPictFormInfo {

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::xcb_render_pictforminfo_t> for &'a XCBRenderPictFormInfo {
type Storage = &'a XCBRenderPictFormInfo;
type Storage = PhantomData<&'a XCBRenderPictFormInfo>;

#[inline]
fn to_glib_none(
&self,
) -> Stash<'a, *mut ffi::xcb_render_pictforminfo_t, &'a XCBRenderPictFormInfo> {
Stash(self.to_raw_none(), *self)
Stash(self.to_raw_none(), PhantomData)
}
}

Expand Down Expand Up @@ -212,11 +214,11 @@ impl XCBScreen {

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::xcb_screen_t> for &'a XCBScreen {
type Storage = &'a XCBScreen;
type Storage = PhantomData<&'a XCBScreen>;

#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_screen_t, &'a XCBScreen> {
Stash(self.to_raw_none(), *self)
Stash(self.to_raw_none(), PhantomData)
}
}

Expand Down Expand Up @@ -374,11 +376,11 @@ impl XCBVisualType {

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::xcb_visualtype_t> for &'a XCBVisualType {
type Storage = &'a XCBVisualType;
type Storage = PhantomData<&'a XCBVisualType>;

#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_visualtype_t, &'a XCBVisualType> {
Stash(self.to_raw_none(), *self)
Stash(self.to_raw_none(), PhantomData)
}
}

Expand Down
6 changes: 3 additions & 3 deletions gio/src/io_extension.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{fmt, ptr};
use std::{fmt, marker::PhantomData, ptr};

use glib::{translate::*, Type};

Expand Down Expand Up @@ -35,11 +35,11 @@ impl FromGlibPtrNone<*mut ffi::GIOExtension> for IOExtension {
}

impl<'a> ToGlibPtr<'a, *mut ffi::GIOExtension> for &'a IOExtension {
type Storage = &'a IOExtension;
type Storage = PhantomData<&'a IOExtension>;

#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut ffi::GIOExtension, &'a IOExtension> {
Stash(self.0.as_ptr() as *mut ffi::GIOExtension, *self)
Stash(self.0.as_ptr() as *mut ffi::GIOExtension, PhantomData)
}
}

Expand Down
6 changes: 3 additions & 3 deletions gio/src/io_extension_point.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{fmt, ptr};
use std::{fmt, marker::PhantomData, ptr};

use glib::{translate::*, Type};

Expand Down Expand Up @@ -68,11 +68,11 @@ impl FromGlibPtrNone<*mut ffi::GIOExtensionPoint> for IOExtensionPoint {
}

impl<'a> ToGlibPtr<'a, *mut ffi::GIOExtensionPoint> for &'a IOExtensionPoint {
type Storage = &'a IOExtensionPoint;
type Storage = PhantomData<&'a IOExtensionPoint>;

#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut ffi::GIOExtensionPoint, &'a IOExtensionPoint> {
Stash(self.0.as_ptr() as *mut ffi::GIOExtensionPoint, *self)
Stash(self.0.as_ptr() as *mut ffi::GIOExtensionPoint, PhantomData)
}
}

Expand Down
8 changes: 4 additions & 4 deletions glib-macros/src/boxed_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
}

impl<'a> #crate_ident::translate::ToGlibPtr<'a, *const #name> for #name {
type Storage = &'a Self;
type Storage = std::marker::PhantomData<&'a Self>;

#[inline]
fn to_glib_none(&'a self) -> #crate_ident::translate::Stash<'a, *const #name, Self> {
#crate_ident::translate::Stash(self as *const #name, self)
#crate_ident::translate::Stash(self as *const #name, std::marker::PhantomData)
}

#[inline]
Expand All @@ -223,11 +223,11 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
}

impl<'a> #crate_ident::translate::ToGlibPtr<'a, *mut #name> for #name {
type Storage = &'a Self;
type Storage = std::marker::PhantomData<&'a Self>;

#[inline]
fn to_glib_none(&'a self) -> #crate_ident::translate::Stash<'a, *mut #name, Self> {
#crate_ident::translate::Stash(self as *const #name as *mut _, self)
#crate_ident::translate::Stash(self as *const #name as *mut _, std::marker::PhantomData)
}

#[inline]
Expand Down
Loading

0 comments on commit d983c4e

Please sign in to comment.