Skip to content

Commit d983c4e

Browse files
authored
Merge pull request #865 from sdroege/stash-phantom-data
Various `Stash` / `to_glib_none()` related optimizations and bugfixes
2 parents a796130 + e9e7a89 commit d983c4e

27 files changed

+181
-163
lines changed

cairo/src/context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

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

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

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

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

7981
#[inline]

cairo/src/device.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#[cfg(any(feature = "script", feature = "dox"))]
44
use std::ffi::CString;
5+
#[cfg(feature = "use_glib")]
6+
use std::marker::PhantomData;
57
#[cfg(any(feature = "script", feature = "dox"))]
68
use std::path::Path;
79
use std::{fmt, ptr};
@@ -305,11 +307,11 @@ impl IntoGlibPtr<*mut ffi::cairo_device_t> for Device {
305307

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

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

315317
#[inline]

cairo/src/matrices.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

33
use std::fmt;
4+
#[cfg(feature = "use_glib")]
5+
use std::marker::PhantomData;
46

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

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

186188
#[inline]
187189
fn to_glib_none(&'a self) -> Stash<'a, *const ffi::cairo_matrix_t, Self> {
188-
let ptr: *const Matrix = self;
189-
Stash(ptr as *const ffi::cairo_matrix_t, self)
190+
Stash(
191+
self as *const Matrix as *const ffi::cairo_matrix_t,
192+
PhantomData,
193+
)
190194
}
191195
}
192196

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

198202
#[inline]
199203
fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::cairo_matrix_t, Self> {
200-
let ptr: *mut Matrix = &mut *self;
201-
StashMut(ptr as *mut ffi::cairo_matrix_t, self)
204+
StashMut(self as *mut Matrix as *mut ffi::cairo_matrix_t, PhantomData)
202205
}
203206
}
204207

cairo/src/rectangle.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::fmt;
44
#[cfg(feature = "use_glib")]
5-
use std::mem;
5+
use std::{marker::PhantomData, mem};
66

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

7575
#[inline]
7676
fn to_glib_none(&'a self) -> Stash<'a, *const ffi::cairo_rectangle_t, Self> {
77-
let ptr: *const Rectangle = self;
78-
Stash(ptr as *const ffi::cairo_rectangle_t, self)
77+
Stash(
78+
self as *const Rectangle as *const ffi::cairo_rectangle_t,
79+
PhantomData,
80+
)
7981
}
8082
}
8183

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

8789
#[inline]
8890
fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::cairo_rectangle_t, Self> {
89-
let ptr: *mut Rectangle = &mut *self;
90-
StashMut(ptr as *mut ffi::cairo_rectangle_t, self)
91+
StashMut(
92+
self as *mut Rectangle as *mut ffi::cairo_rectangle_t,
93+
PhantomData,
94+
)
9195
}
9296
}
9397

cairo/src/rectangle_int.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::fmt;
44
#[cfg(feature = "use_glib")]
5-
use std::mem;
5+
use std::{marker::PhantomData, mem};
66

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

7575
#[inline]
7676
fn to_glib_none(&'a self) -> Stash<'a, *const ffi::cairo_rectangle_int_t, Self> {
77-
let ptr: *const RectangleInt = self;
78-
Stash(ptr as *const ffi::cairo_rectangle_int_t, self)
77+
Stash(
78+
self as *const RectangleInt as *const ffi::cairo_rectangle_int_t,
79+
PhantomData,
80+
)
7981
}
8082
}
8183

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

8789
#[inline]
8890
fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::cairo_rectangle_int_t, Self> {
89-
let ptr: *mut RectangleInt = &mut *self;
90-
StashMut(ptr as *mut ffi::cairo_rectangle_int_t, self)
91+
StashMut(
92+
self as *mut RectangleInt as *mut ffi::cairo_rectangle_int_t,
93+
PhantomData,
94+
)
9195
}
9296
}
9397

cairo/src/region.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

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

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

2729
#[inline]
28-
fn to_glib_none(&self) -> Stash<'a, *mut ffi::cairo_region_t, &'a Region> {
29-
Stash(self.0.as_ptr(), *self)
30+
fn to_glib_none(&self) -> Stash<'a, *mut ffi::cairo_region_t, Self> {
31+
Stash(self.0.as_ptr(), PhantomData)
3032
}
3133

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

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

cairo/src/surface.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

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

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

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

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

302304
#[inline]

cairo/src/surface_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ macro_rules! declare_surface {
4545

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

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

cairo/src/xcb.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

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

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

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

6971
#[inline]
7072
fn to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_connection_t, &'a XCBConnection> {
71-
Stash(self.to_raw_none(), *self)
73+
Stash(self.to_raw_none(), PhantomData)
7274
}
7375
}
7476

@@ -137,13 +139,13 @@ impl XCBRenderPictFormInfo {
137139

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

142144
#[inline]
143145
fn to_glib_none(
144146
&self,
145147
) -> Stash<'a, *mut ffi::xcb_render_pictforminfo_t, &'a XCBRenderPictFormInfo> {
146-
Stash(self.to_raw_none(), *self)
148+
Stash(self.to_raw_none(), PhantomData)
147149
}
148150
}
149151

@@ -212,11 +214,11 @@ impl XCBScreen {
212214

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

217219
#[inline]
218220
fn to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_screen_t, &'a XCBScreen> {
219-
Stash(self.to_raw_none(), *self)
221+
Stash(self.to_raw_none(), PhantomData)
220222
}
221223
}
222224

@@ -374,11 +376,11 @@ impl XCBVisualType {
374376

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

379381
#[inline]
380382
fn to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_visualtype_t, &'a XCBVisualType> {
381-
Stash(self.to_raw_none(), *self)
383+
Stash(self.to_raw_none(), PhantomData)
382384
}
383385
}
384386

gio/src/io_extension.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::{fmt, ptr};
3+
use std::{fmt, marker::PhantomData, ptr};
44

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

@@ -35,11 +35,11 @@ impl FromGlibPtrNone<*mut ffi::GIOExtension> for IOExtension {
3535
}
3636

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

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

0 commit comments

Comments
 (0)