Skip to content

Commit 575d3ef

Browse files
authored
Merge pull request #575 from ranfdev/improve_generics_macros
use Self where possible in wrapper macros
2 parents ca43ffe + ba11fb5 commit 575d3ef

File tree

4 files changed

+75
-75
lines changed

4 files changed

+75
-75
lines changed

glib/src/boxed.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ macro_rules! glib_boxed_wrapper {
3232
$(#[$attr])*
3333
#[repr(transparent)]
3434
$visibility struct $name $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? {
35-
inner: $crate::boxed::Boxed<$ffi_name, $name $(<$($generic),+>)?>,
35+
inner: $crate::boxed::Boxed<$ffi_name, Self>,
3636
phantom: std::marker::PhantomData<($($($generic),+)?)>,
3737
}
3838

@@ -53,7 +53,7 @@ macro_rules! glib_boxed_wrapper {
5353

5454
#[doc(hidden)]
5555
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtr<'a, *const $ffi_name> for $name $(<$($generic),+>)? {
56-
type Storage = &'a $crate::boxed::Boxed<$ffi_name, $name $(<$($generic),+>)?>;
56+
type Storage = &'a $crate::boxed::Boxed<$ffi_name, Self>;
5757

5858
#[inline]
5959
fn to_glib_none(&'a self) -> $crate::translate::Stash<'a, *const $ffi_name, Self> {
@@ -69,7 +69,7 @@ macro_rules! glib_boxed_wrapper {
6969

7070
#[doc(hidden)]
7171
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtrMut<'a, *mut $ffi_name> for $name $(<$($generic),+>)? {
72-
type Storage = &'a mut $crate::boxed::Boxed<$ffi_name, $name $(<$($generic),+>)?>;
72+
type Storage = &'a mut $crate::boxed::Boxed<$ffi_name, Self>;
7373

7474
#[inline]
7575
fn to_glib_none_mut(&'a mut self) -> $crate::translate::StashMut<'a, *mut $ffi_name, Self> {
@@ -80,17 +80,17 @@ macro_rules! glib_boxed_wrapper {
8080

8181
#[doc(hidden)]
8282
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *mut *const $ffi_name> for $name $(<$($generic),+>)? {
83-
type Storage = (Vec<$crate::translate::Stash<'a, *const $ffi_name, $name $(<$($generic),+>)?>>, Option<Vec<*const $ffi_name>>);
83+
type Storage = (Vec<$crate::translate::Stash<'a, *const $ffi_name, Self>>, Option<Vec<*const $ffi_name>>);
8484

85-
fn to_glib_none_from_slice(t: &'a [$name $(<$($generic),+>)?]) -> (*mut *const $ffi_name, Self::Storage) {
85+
fn to_glib_none_from_slice(t: &'a [Self]) -> (*mut *const $ffi_name, Self::Storage) {
8686
let v: Vec<_> = t.iter().map(|s| $crate::translate::ToGlibPtr::to_glib_none(s)).collect();
8787
let mut v_ptr: Vec<_> = v.iter().map(|s| s.0).collect();
8888
v_ptr.push(std::ptr::null_mut() as *const $ffi_name);
8989

9090
(v_ptr.as_ptr() as *mut *const $ffi_name, (v, Some(v_ptr)))
9191
}
9292

93-
fn to_glib_container_from_slice(t: &'a [$name $(<$($generic),+>)?]) -> (*mut *const $ffi_name, Self::Storage) {
93+
fn to_glib_container_from_slice(t: &'a [Self]) -> (*mut *const $ffi_name, Self::Storage) {
9494
let v: Vec<_> = t.iter().map(|s| $crate::translate::ToGlibPtr::to_glib_none(s)).collect();
9595

9696
let v_ptr = unsafe {
@@ -106,7 +106,7 @@ macro_rules! glib_boxed_wrapper {
106106
(v_ptr, (v, None))
107107
}
108108

109-
fn to_glib_full_from_slice(t: &[$name $(<$($generic),+>)?]) -> *mut *const $ffi_name {
109+
fn to_glib_full_from_slice(t: &[Self]) -> *mut *const $ffi_name {
110110
unsafe {
111111
let v_ptr = $crate::ffi::g_malloc0(std::mem::size_of::<*const $ffi_name>() * (t.len() + 1)) as *mut *const $ffi_name;
112112

@@ -121,19 +121,19 @@ macro_rules! glib_boxed_wrapper {
121121

122122
#[doc(hidden)]
123123
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *const *const $ffi_name> for $name $(<$($generic),+>)? {
124-
type Storage = (Vec<$crate::translate::Stash<'a, *const $ffi_name, $name $(<$($generic),+>)?>>, Option<Vec<*const $ffi_name>>);
124+
type Storage = (Vec<$crate::translate::Stash<'a, *const $ffi_name, Self>>, Option<Vec<*const $ffi_name>>);
125125

126-
fn to_glib_none_from_slice(t: &'a [$name $(<$($generic),+>)?]) -> (*const *const $ffi_name, Self::Storage) {
126+
fn to_glib_none_from_slice(t: &'a [Self]) -> (*const *const $ffi_name, Self::Storage) {
127127
let (ptr, stash) = $crate::translate::ToGlibContainerFromSlice::<'a, *mut *const $ffi_name>::to_glib_none_from_slice(t);
128128
(ptr as *const *const $ffi_name, stash)
129129
}
130130

131-
fn to_glib_container_from_slice(_: &'a [$name $(<$($generic),+>)?]) -> (*const *const $ffi_name, Self::Storage) {
131+
fn to_glib_container_from_slice(_: &'a [Self]) -> (*const *const $ffi_name, Self::Storage) {
132132
// Can't have consumer free a *const pointer
133133
unimplemented!()
134134
}
135135

136-
fn to_glib_full_from_slice(_: &[$name $(<$($generic),+>)?]) -> *const *const $ffi_name {
136+
fn to_glib_full_from_slice(_: &[Self]) -> *const *const $ffi_name {
137137
// Can't have consumer free a *const pointer
138138
unimplemented!()
139139
}
@@ -200,7 +200,7 @@ macro_rules! glib_boxed_wrapper {
200200
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow<*const $ffi_name> for $name $(<$($generic),+>)? {
201201
#[inline]
202202
unsafe fn from_glib_borrow(ptr: *const $ffi_name) -> $crate::translate::Borrowed<Self> {
203-
$crate::translate::from_glib_borrow::<_, $name $(<$($generic),+>)?>(ptr as *mut $ffi_name)
203+
$crate::translate::from_glib_borrow::<_, Self>(ptr as *mut $ffi_name)
204204
}
205205
}
206206

@@ -266,7 +266,7 @@ macro_rules! glib_boxed_wrapper {
266266

267267
#[doc(hidden)]
268268
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::value::ValueType for $name $(<$($generic),+>)? {
269-
type Type = $name $(<$($generic),+>)?;
269+
type Type = Self;
270270
}
271271

272272
#[doc(hidden)]
@@ -279,7 +279,7 @@ macro_rules! glib_boxed_wrapper {
279279
unsafe fn from_value(value: &'a $crate::Value) -> Self {
280280
let ptr = $crate::gobject_ffi::g_value_dup_boxed($crate::translate::ToGlibPtr::to_glib_none(value).0);
281281
assert!(!ptr.is_null());
282-
<$name $(<$($generic),+>)? as $crate::translate::FromGlibPtrFull<*mut $ffi_name>>::from_glib_full(ptr as *mut $ffi_name)
282+
<Self as $crate::translate::FromGlibPtrFull<*mut $ffi_name>>::from_glib_full(ptr as *mut $ffi_name)
283283
}
284284
}
285285

@@ -288,7 +288,7 @@ macro_rules! glib_boxed_wrapper {
288288
type Checker = $crate::value::GenericValueTypeOrNoneChecker<Self>;
289289

290290
unsafe fn from_value(value: &'a $crate::Value) -> Self {
291-
assert_eq!(std::mem::size_of::<$name $(<$($generic),+>)?>(), std::mem::size_of::<$crate::ffi::gpointer>());
291+
assert_eq!(std::mem::size_of::<Self>(), std::mem::size_of::<$crate::ffi::gpointer>());
292292
let value = &*(value as *const $crate::Value as *const $crate::gobject_ffi::GValue);
293293
let ptr = &value.data[0].v_pointer as *const $crate::ffi::gpointer as *const *const $ffi_name;
294294
assert!(!(*ptr).is_null());
@@ -300,7 +300,7 @@ macro_rules! glib_boxed_wrapper {
300300
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::value::ToValue for $name $(<$($generic),+>)? {
301301
fn to_value(&self) -> $crate::Value {
302302
unsafe {
303-
let mut value = $crate::Value::from_type(<$name $(<$($generic),+>)? as $crate::StaticType>::static_type());
303+
let mut value = $crate::Value::from_type(<Self as $crate::StaticType>::static_type());
304304
$crate::gobject_ffi::g_value_take_boxed(
305305
$crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
306306
$crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_full(self) as *mut _,
@@ -310,7 +310,7 @@ macro_rules! glib_boxed_wrapper {
310310
}
311311

312312
fn value_type(&self) -> $crate::Type {
313-
<$name $(<$($generic),+>)? as $crate::StaticType>::static_type()
313+
<Self as $crate::StaticType>::static_type()
314314
}
315315
}
316316

glib/src/boxed_inline.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ macro_rules! glib_boxed_inline_wrapper {
181181

182182
#[doc(hidden)]
183183
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtr<'a, *const $ffi_name> for $name $(<$($generic),+>)? {
184-
type Storage = &'a $name $(<$($generic),+>)?;
184+
type Storage = &'a Self;
185185

186186
#[inline]
187187
fn to_glib_none(&'a self) -> $crate::translate::Stash<'a, *const $ffi_name, Self> {
@@ -199,7 +199,7 @@ macro_rules! glib_boxed_inline_wrapper {
199199

200200
#[doc(hidden)]
201201
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibPtrMut<'a, *mut $ffi_name> for $name $(<$($generic),+>)? {
202-
type Storage = &'a mut $name $(<$($generic),+>)?;
202+
type Storage = &'a mut Self;
203203

204204
#[inline]
205205
fn to_glib_none_mut(&'a mut self) -> $crate::translate::StashMut<'a, *mut $ffi_name, Self> {
@@ -212,14 +212,14 @@ macro_rules! glib_boxed_inline_wrapper {
212212
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *mut *const $ffi_name> for $name $(<$($generic),+>)? {
213213
type Storage = Option<Vec<*const $ffi_name>>;
214214

215-
fn to_glib_none_from_slice(t: &'a [$name $(<$($generic),+>)?]) -> (*mut *const $ffi_name, Self::Storage) {
215+
fn to_glib_none_from_slice(t: &'a [Self]) -> (*mut *const $ffi_name, Self::Storage) {
216216
let mut v: Vec<_> = t.iter().map(|s| &s.inner as *const $ffi_name).collect();
217217
v.push(std::ptr::null_mut() as *const $ffi_name);
218218

219219
(v.as_mut_ptr(), Some(v))
220220
}
221221

222-
fn to_glib_container_from_slice(t: &'a [$name $(<$($generic),+>)?]) -> (*mut *const $ffi_name, Self::Storage) {
222+
fn to_glib_container_from_slice(t: &'a [Self]) -> (*mut *const $ffi_name, Self::Storage) {
223223
let v_ptr = unsafe {
224224
let v_ptr = $crate::ffi::g_malloc0(std::mem::size_of::<*const $ffi_name>() * (t.len() + 1)) as *mut *const $ffi_name;
225225

@@ -233,7 +233,7 @@ macro_rules! glib_boxed_inline_wrapper {
233233
(v_ptr, None)
234234
}
235235

236-
fn to_glib_full_from_slice(t: &[$name $(<$($generic),+>)?]) -> *mut *const $ffi_name {
236+
fn to_glib_full_from_slice(t: &[Self]) -> *mut *const $ffi_name {
237237
unsafe {
238238
let v_ptr = $crate::ffi::g_malloc0(std::mem::size_of::<*const $ffi_name>() * (t.len() + 1)) as *mut *const $ffi_name;
239239

@@ -250,38 +250,38 @@ macro_rules! glib_boxed_inline_wrapper {
250250
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *const *const $ffi_name> for $name $(<$($generic),+>)? {
251251
type Storage = Option<Vec<*const $ffi_name>>;
252252

253-
fn to_glib_none_from_slice(t: &'a [$name $(<$($generic),+>)?]) -> (*const *const $ffi_name, Self::Storage) {
253+
fn to_glib_none_from_slice(t: &'a [Self]) -> (*const *const $ffi_name, Self::Storage) {
254254
let (ptr, stash) = $crate::translate::ToGlibContainerFromSlice::<'a, *mut *const $ffi_name>::to_glib_none_from_slice(t);
255255
(ptr as *const *const $ffi_name, stash)
256256
}
257257

258-
fn to_glib_container_from_slice(_: &'a [$name $(<$($generic),+>)?]) -> (*const *const $ffi_name, Self::Storage) {
258+
fn to_glib_container_from_slice(_: &'a [Self]) -> (*const *const $ffi_name, Self::Storage) {
259259
// Can't have consumer free a *const pointer
260260
unimplemented!()
261261
}
262262

263-
fn to_glib_full_from_slice(_: &[$name $(<$($generic),+>)?]) -> *const *const $ffi_name {
263+
fn to_glib_full_from_slice(_: &[Self]) -> *const *const $ffi_name {
264264
// Can't have consumer free a *const pointer
265265
unimplemented!()
266266
}
267267
}
268268

269269
#[doc(hidden)]
270270
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *mut $ffi_name> for $name $(<$($generic),+>)? {
271-
type Storage = Option<&'a [$name $(<$($generic),+>)?]>;
271+
type Storage = Option<&'a [Self]>;
272272

273-
fn to_glib_none_from_slice(t: &'a [$name $(<$($generic),+>)?]) -> (*mut $ffi_name, Self::Storage) {
273+
fn to_glib_none_from_slice(t: &'a [Self]) -> (*mut $ffi_name, Self::Storage) {
274274
(t.as_ptr() as *mut $ffi_name, Some(t))
275275
}
276276

277-
fn to_glib_container_from_slice(t: &'a [$name $(<$($generic),+>)?]) -> (*mut $ffi_name, Self::Storage) {
277+
fn to_glib_container_from_slice(t: &'a [Self]) -> (*mut $ffi_name, Self::Storage) {
278278
(
279279
$crate::translate::ToGlibContainerFromSlice::<'a, *mut $ffi_name>::to_glib_full_from_slice(t),
280280
None,
281281
)
282282
}
283283

284-
fn to_glib_full_from_slice(t: &[$name $(<$($generic),+>)?]) -> *mut $ffi_name {
284+
fn to_glib_full_from_slice(t: &[Self]) -> *mut $ffi_name {
285285
let v_ptr = unsafe {
286286
let v_ptr = $crate::ffi::g_malloc0(std::mem::size_of::<$ffi_name>()) as *mut $ffi_name;
287287

@@ -299,19 +299,19 @@ macro_rules! glib_boxed_inline_wrapper {
299299

300300
#[doc(hidden)]
301301
impl<'a $(, $($generic $(: $bound $(+ $bound2)*)?),+)?> $crate::translate::ToGlibContainerFromSlice<'a, *const $ffi_name> for $name $(<$($generic),+>)? {
302-
type Storage = Option<&'a [$name $(<$($generic),+>)?]>;
302+
type Storage = Option<&'a [Self]>;
303303

304-
fn to_glib_none_from_slice(t: &'a [$name $(<$($generic),+>)?]) -> (*const $ffi_name, Self::Storage) {
304+
fn to_glib_none_from_slice(t: &'a [Self]) -> (*const $ffi_name, Self::Storage) {
305305
let (ptr, stash) = $crate::translate::ToGlibContainerFromSlice::<'a, *mut $ffi_name>::to_glib_none_from_slice(t);
306306
(ptr as *const $ffi_name, stash)
307307
}
308308

309-
fn to_glib_container_from_slice(_: &'a [$name $(<$($generic),+>)?]) -> (*const $ffi_name, Self::Storage) {
309+
fn to_glib_container_from_slice(_: &'a [Self]) -> (*const $ffi_name, Self::Storage) {
310310
// Can't have consumer free a *const pointer
311311
unimplemented!()
312312
}
313313

314-
fn to_glib_full_from_slice(_: &[$name $(<$($generic),+>)?]) -> *const $ffi_name {
314+
fn to_glib_full_from_slice(_: &[Self]) -> *const $ffi_name {
315315
// Can't have consumer free a *const pointer
316316
unimplemented!()
317317
}
@@ -323,7 +323,7 @@ macro_rules! glib_boxed_inline_wrapper {
323323
unsafe fn from_glib_none(ptr: *mut $ffi_name) -> Self {
324324
assert!(!ptr.is_null());
325325

326-
let mut v = <$name $(<$($generic),+>)? as $crate::translate::Uninitialized>::uninitialized();
326+
let mut v = <Self as $crate::translate::Uninitialized>::uninitialized();
327327
let copy_into = |$copy_into_arg_dest: *mut $ffi_name, $copy_into_arg_src: *const $ffi_name| $copy_into_expr;
328328
copy_into(&mut v.inner as *mut $ffi_name, ptr as *const $ffi_name);
329329

@@ -335,7 +335,7 @@ macro_rules! glib_boxed_inline_wrapper {
335335
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrNone<*const $ffi_name> for $name $(<$($generic),+>)? {
336336
#[inline]
337337
unsafe fn from_glib_none(ptr: *const $ffi_name) -> Self {
338-
$crate::translate::from_glib_none::<_, $name $(<$($generic),+>)?>(ptr as *mut $ffi_name)
338+
$crate::translate::from_glib_none::<_, Self>(ptr as *mut $ffi_name)
339339
}
340340
}
341341

@@ -345,7 +345,7 @@ macro_rules! glib_boxed_inline_wrapper {
345345
unsafe fn from_glib_full(ptr: *mut $ffi_name) -> Self {
346346
assert!(!ptr.is_null());
347347

348-
let mut v = <$name $(<$($generic),+>)? as $crate::translate::Uninitialized>::uninitialized();
348+
let mut v = <Self as $crate::translate::Uninitialized>::uninitialized();
349349
let copy_into = |$copy_into_arg_dest: *mut $ffi_name, $copy_into_arg_src: *const $ffi_name| $copy_into_expr;
350350
copy_into(&mut v.inner as *mut $ffi_name, ptr as *const $ffi_name);
351351

@@ -360,7 +360,7 @@ macro_rules! glib_boxed_inline_wrapper {
360360
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrFull<*const $ffi_name> for $name $(<$($generic),+>)? {
361361
#[inline]
362362
unsafe fn from_glib_full(ptr: *const $ffi_name) -> Self {
363-
$crate::translate::from_glib_full::<_, $name $(<$($generic),+>)?>(ptr as *mut $ffi_name)
363+
$crate::translate::from_glib_full::<_, Self>(ptr as *mut $ffi_name)
364364
}
365365
}
366366

@@ -381,7 +381,7 @@ macro_rules! glib_boxed_inline_wrapper {
381381
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::FromGlibPtrBorrow<*const $ffi_name> for $name $(<$($generic),+>)? {
382382
#[inline]
383383
unsafe fn from_glib_borrow(ptr: *const $ffi_name) -> $crate::translate::Borrowed<Self> {
384-
$crate::translate::from_glib_borrow::<_, $name $(<$($generic),+>)?>(ptr as *mut $ffi_name)
384+
$crate::translate::from_glib_borrow::<_, Self>(ptr as *mut $ffi_name)
385385
}
386386
}
387387

@@ -496,7 +496,7 @@ macro_rules! glib_boxed_inline_wrapper {
496496

497497
#[doc(hidden)]
498498
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::value::ValueType for $name $(<$($generic),+>)? {
499-
type Type = $name $(<$($generic),+>)?;
499+
type Type = Self;
500500
}
501501

502502
#[doc(hidden)]
@@ -506,7 +506,7 @@ macro_rules! glib_boxed_inline_wrapper {
506506
unsafe fn from_value(value: &'a $crate::Value) -> Self {
507507
let ptr = $crate::gobject_ffi::g_value_get_boxed($crate::translate::ToGlibPtr::to_glib_none(value).0);
508508
assert!(!ptr.is_null());
509-
<$name $(<$($generic),+>)? as $crate::translate::FromGlibPtrNone<*const $ffi_name>>::from_glib_none(ptr as *const $ffi_name)
509+
<Self as $crate::translate::FromGlibPtrNone<*const $ffi_name>>::from_glib_none(ptr as *const $ffi_name)
510510
}
511511
}
512512

@@ -525,7 +525,7 @@ macro_rules! glib_boxed_inline_wrapper {
525525
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::value::ToValue for $name $(<$($generic),+>)? {
526526
fn to_value(&self) -> $crate::Value {
527527
unsafe {
528-
let mut value = $crate::Value::from_type(<$name $(<$($generic),+>)? as $crate::StaticType>::static_type());
528+
let mut value = $crate::Value::from_type(<Self as $crate::StaticType>::static_type());
529529
$crate::gobject_ffi::g_value_set_boxed(
530530
$crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
531531
$crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(self).0 as *mut _,
@@ -535,7 +535,7 @@ macro_rules! glib_boxed_inline_wrapper {
535535
}
536536

537537
fn value_type(&self) -> $crate::Type {
538-
<$name $(<$($generic),+>)? as $crate::StaticType>::static_type()
538+
<Self as $crate::StaticType>::static_type()
539539
}
540540
}
541541

0 commit comments

Comments
 (0)