Skip to content

Commit fe1a41c

Browse files
authored
Merge pull request #1171 from ranfdev/cleanups
Use associated type in memory managers
2 parents cab7e1c + a682438 commit fe1a41c

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

glib/src/boxed.rs

+28-24
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,17 @@ macro_rules! glib_boxed_wrapper {
420420

421421
(@memory_manager_impl $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $ffi_name:ty, @copy $copy_arg:ident $copy_expr:expr, @free $free_arg:ident $free_expr:expr) => {
422422
#[doc(hidden)]
423-
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::boxed::BoxedMemoryManager<$ffi_name> for $name $(<$($generic),+>)? {
423+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::boxed::BoxedMemoryManager for $name $(<$($generic),+>)? {
424+
type Target = $ffi_name;
425+
424426
#[inline]
425-
unsafe fn copy($copy_arg: *const $ffi_name) -> *mut $ffi_name {
427+
unsafe fn copy($copy_arg: *const Self::Target) -> *mut Self::Target {
426428
$copy_expr
427429
}
428430

429431
#[inline]
430432
#[allow(clippy::no_effect)]
431-
unsafe fn free($free_arg: *mut $ffi_name) {
433+
unsafe fn free($free_arg: *mut Self::Target) {
432434
$free_expr;
433435
}
434436
}
@@ -437,21 +439,23 @@ macro_rules! glib_boxed_wrapper {
437439

438440
// The safety docs really belong in the wrapper!() macro for Boxed<T>
439441
/// Memory management functions for a boxed type.
440-
pub trait BoxedMemoryManager<T>: 'static {
442+
pub trait BoxedMemoryManager: 'static {
443+
type Target;
444+
441445
/// Makes a copy.
442-
unsafe fn copy(ptr: *const T) -> *mut T;
446+
unsafe fn copy(ptr: *const Self::Target) -> *mut Self::Target;
443447
/// Frees the object.
444-
unsafe fn free(ptr: *mut T);
448+
unsafe fn free(ptr: *mut Self::Target);
445449
}
446450

447451
/// Encapsulates memory management logic for boxed types.
448452
#[repr(transparent)]
449-
pub struct Boxed<T: 'static, MM: BoxedMemoryManager<T>> {
453+
pub struct Boxed<T: 'static, MM: BoxedMemoryManager<Target = T>> {
450454
inner: ptr::NonNull<T>,
451455
_dummy: PhantomData<*mut MM>,
452456
}
453457

454-
impl<'a, T: 'static, MM: BoxedMemoryManager<T>> ToGlibPtr<'a, *const T> for Boxed<T, MM> {
458+
impl<'a, T: 'static, MM: BoxedMemoryManager<Target = T>> ToGlibPtr<'a, *const T> for Boxed<T, MM> {
455459
type Storage = PhantomData<&'a Self>;
456460

457461
#[inline]
@@ -467,7 +471,7 @@ impl<'a, T: 'static, MM: BoxedMemoryManager<T>> ToGlibPtr<'a, *const T> for Boxe
467471
}
468472
}
469473

470-
impl<'a, T: 'static, MM: BoxedMemoryManager<T>> ToGlibPtrMut<'a, *mut T> for Boxed<T, MM> {
474+
impl<'a, T: 'static, MM: BoxedMemoryManager<Target = T>> ToGlibPtrMut<'a, *mut T> for Boxed<T, MM> {
471475
type Storage = PhantomData<&'a mut Self>;
472476

473477
#[inline]
@@ -477,7 +481,7 @@ impl<'a, T: 'static, MM: BoxedMemoryManager<T>> ToGlibPtrMut<'a, *mut T> for Box
477481
}
478482
}
479483

480-
impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrNone<*mut T> for Boxed<T, MM> {
484+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> FromGlibPtrNone<*mut T> for Boxed<T, MM> {
481485
#[inline]
482486
unsafe fn from_glib_none(ptr: *mut T) -> Self {
483487
debug_assert!(!ptr.is_null());
@@ -486,7 +490,7 @@ impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrNone<*mut T> for Boxed<T,
486490
}
487491
}
488492

489-
impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrNone<*const T> for Boxed<T, MM> {
493+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> FromGlibPtrNone<*const T> for Boxed<T, MM> {
490494
#[inline]
491495
unsafe fn from_glib_none(ptr: *const T) -> Self {
492496
debug_assert!(!ptr.is_null());
@@ -495,7 +499,7 @@ impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrNone<*const T> for Boxed<
495499
}
496500
}
497501

498-
impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrFull<*mut T> for Boxed<T, MM> {
502+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> FromGlibPtrFull<*mut T> for Boxed<T, MM> {
499503
#[inline]
500504
unsafe fn from_glib_full(ptr: *mut T) -> Self {
501505
debug_assert!(!ptr.is_null());
@@ -506,7 +510,7 @@ impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrFull<*mut T> for Boxed<T,
506510
}
507511
}
508512

509-
impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrFull<*const T> for Boxed<T, MM> {
513+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> FromGlibPtrFull<*const T> for Boxed<T, MM> {
510514
#[inline]
511515
unsafe fn from_glib_full(ptr: *const T) -> Self {
512516
debug_assert!(!ptr.is_null());
@@ -517,7 +521,7 @@ impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrFull<*const T> for Boxed<
517521
}
518522
}
519523

520-
impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrBorrow<*mut T> for Boxed<T, MM> {
524+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> FromGlibPtrBorrow<*mut T> for Boxed<T, MM> {
521525
#[inline]
522526
unsafe fn from_glib_borrow(ptr: *mut T) -> Borrowed<Self> {
523527
debug_assert!(!ptr.is_null());
@@ -528,7 +532,7 @@ impl<T: 'static, MM: BoxedMemoryManager<T>> FromGlibPtrBorrow<*mut T> for Boxed<
528532
}
529533
}
530534

531-
impl<T: 'static, MM: BoxedMemoryManager<T>> Drop for Boxed<T, MM> {
535+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> Drop for Boxed<T, MM> {
532536
#[inline]
533537
fn drop(&mut self) {
534538
unsafe {
@@ -537,36 +541,36 @@ impl<T: 'static, MM: BoxedMemoryManager<T>> Drop for Boxed<T, MM> {
537541
}
538542
}
539543

540-
impl<T: 'static, MM: BoxedMemoryManager<T>> fmt::Debug for Boxed<T, MM> {
544+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> fmt::Debug for Boxed<T, MM> {
541545
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
542546
f.debug_struct("Boxed").field("inner", &self.inner).finish()
543547
}
544548
}
545549

546-
impl<T, MM: BoxedMemoryManager<T>> PartialOrd for Boxed<T, MM> {
550+
impl<T, MM: BoxedMemoryManager<Target = T>> PartialOrd for Boxed<T, MM> {
547551
#[inline]
548552
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
549553
self.to_glib_none().0.partial_cmp(&other.to_glib_none().0)
550554
}
551555
}
552556

553-
impl<T, MM: BoxedMemoryManager<T>> Ord for Boxed<T, MM> {
557+
impl<T, MM: BoxedMemoryManager<Target = T>> Ord for Boxed<T, MM> {
554558
#[inline]
555559
fn cmp(&self, other: &Self) -> cmp::Ordering {
556560
self.to_glib_none().0.cmp(&other.to_glib_none().0)
557561
}
558562
}
559563

560-
impl<T, MM: BoxedMemoryManager<T>> PartialEq for Boxed<T, MM> {
564+
impl<T, MM: BoxedMemoryManager<Target = T>> PartialEq for Boxed<T, MM> {
561565
#[inline]
562566
fn eq(&self, other: &Self) -> bool {
563567
self.to_glib_none().0 == other.to_glib_none().0
564568
}
565569
}
566570

567-
impl<T, MM: BoxedMemoryManager<T>> Eq for Boxed<T, MM> {}
571+
impl<T, MM: BoxedMemoryManager<Target = T>> Eq for Boxed<T, MM> {}
568572

569-
impl<T, MM: BoxedMemoryManager<T>> Hash for Boxed<T, MM> {
573+
impl<T, MM: BoxedMemoryManager<Target = T>> Hash for Boxed<T, MM> {
570574
#[inline]
571575
fn hash<H>(&self, state: &mut H)
572576
where
@@ -576,14 +580,14 @@ impl<T, MM: BoxedMemoryManager<T>> Hash for Boxed<T, MM> {
576580
}
577581
}
578582

579-
impl<T: 'static, MM: BoxedMemoryManager<T>> Clone for Boxed<T, MM> {
583+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> Clone for Boxed<T, MM> {
580584
#[inline]
581585
fn clone(&self) -> Self {
582586
unsafe { from_glib_none(self.to_glib_none().0 as *mut T) }
583587
}
584588
}
585589

586-
impl<T: 'static, MM: BoxedMemoryManager<T>> Deref for Boxed<T, MM> {
590+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> Deref for Boxed<T, MM> {
587591
type Target = T;
588592

589593
#[inline]
@@ -595,7 +599,7 @@ impl<T: 'static, MM: BoxedMemoryManager<T>> Deref for Boxed<T, MM> {
595599
}
596600
}
597601

598-
impl<T: 'static, MM: BoxedMemoryManager<T>> DerefMut for Boxed<T, MM> {
602+
impl<T: 'static, MM: BoxedMemoryManager<Target = T>> DerefMut for Boxed<T, MM> {
599603
#[inline]
600604
fn deref_mut(&mut self) -> &mut T {
601605
unsafe {

glib/src/shared.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ macro_rules! glib_shared_wrapper {
5959
}
6060

6161
#[doc(hidden)]
62-
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::shared::SharedMemoryManager<$ffi_name> for $name $(<$($generic),+>)? {
62+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::shared::SharedMemoryManager for $name $(<$($generic),+>)? {
63+
type Target = $ffi_name;
64+
6365
#[inline]
64-
unsafe fn ref_($ref_arg: *mut $ffi_name) {
66+
unsafe fn ref_($ref_arg: *mut Self::Target) {
6567
$ref_expr;
6668
}
6769

6870
#[inline]
6971
#[allow(clippy::no_effect)]
70-
unsafe fn unref($unref_arg: *mut $ffi_name) {
72+
unsafe fn unref($unref_arg: *mut Self::Target) {
7173
$unref_expr;
7274
}
7375
}
@@ -436,29 +438,31 @@ macro_rules! glib_shared_wrapper {
436438
};
437439
}
438440

439-
pub trait SharedMemoryManager<T> {
441+
pub trait SharedMemoryManager {
442+
type Target;
443+
440444
/// # Safety
441445
///
442446
/// Callers are responsible for ensuring that a matching call to `unref`
443447
/// is made at an appropriate time.
444-
unsafe fn ref_(ptr: *mut T);
448+
unsafe fn ref_(ptr: *mut Self::Target);
445449

446450
/// # Safety
447451
///
448452
/// Callers are responsible for ensuring that a matching call to `ref` was
449453
/// made before this is called, and that the pointer is not used after the
450454
/// `unref` call.
451-
unsafe fn unref(ptr: *mut T);
455+
unsafe fn unref(ptr: *mut Self::Target);
452456
}
453457

454458
/// Encapsulates memory management logic for shared types.
455459
#[repr(transparent)]
456-
pub struct Shared<T, MM: SharedMemoryManager<T>> {
460+
pub struct Shared<T, MM: SharedMemoryManager<Target = T>> {
457461
inner: ptr::NonNull<T>,
458462
mm: PhantomData<*const MM>,
459463
}
460464

461-
impl<T, MM: SharedMemoryManager<T>> Drop for Shared<T, MM> {
465+
impl<T, MM: SharedMemoryManager<Target = T>> Drop for Shared<T, MM> {
462466
#[inline]
463467
fn drop(&mut self) {
464468
unsafe {
@@ -467,7 +471,7 @@ impl<T, MM: SharedMemoryManager<T>> Drop for Shared<T, MM> {
467471
}
468472
}
469473

470-
impl<T, MM: SharedMemoryManager<T>> Clone for Shared<T, MM> {
474+
impl<T, MM: SharedMemoryManager<Target = T>> Clone for Shared<T, MM> {
471475
#[inline]
472476
fn clone(&self) -> Self {
473477
unsafe {
@@ -480,38 +484,38 @@ impl<T, MM: SharedMemoryManager<T>> Clone for Shared<T, MM> {
480484
}
481485
}
482486

483-
impl<T, MM: SharedMemoryManager<T>> fmt::Debug for Shared<T, MM> {
487+
impl<T, MM: SharedMemoryManager<Target = T>> fmt::Debug for Shared<T, MM> {
484488
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
485489
f.debug_struct("Shared")
486490
.field("inner", &self.inner)
487491
.finish()
488492
}
489493
}
490494

491-
impl<T, MM: SharedMemoryManager<T>> PartialOrd for Shared<T, MM> {
495+
impl<T, MM: SharedMemoryManager<Target = T>> PartialOrd for Shared<T, MM> {
492496
#[inline]
493497
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
494498
self.inner.partial_cmp(&other.inner)
495499
}
496500
}
497501

498-
impl<T, MM: SharedMemoryManager<T>> Ord for Shared<T, MM> {
502+
impl<T, MM: SharedMemoryManager<Target = T>> Ord for Shared<T, MM> {
499503
#[inline]
500504
fn cmp(&self, other: &Self) -> cmp::Ordering {
501505
self.inner.cmp(&other.inner)
502506
}
503507
}
504508

505-
impl<T, MM: SharedMemoryManager<T>> PartialEq for Shared<T, MM> {
509+
impl<T, MM: SharedMemoryManager<Target = T>> PartialEq for Shared<T, MM> {
506510
#[inline]
507511
fn eq(&self, other: &Self) -> bool {
508512
self.inner == other.inner
509513
}
510514
}
511515

512-
impl<T, MM: SharedMemoryManager<T>> Eq for Shared<T, MM> {}
516+
impl<T, MM: SharedMemoryManager<Target = T>> Eq for Shared<T, MM> {}
513517

514-
impl<T, MM: SharedMemoryManager<T>> Hash for Shared<T, MM> {
518+
impl<T, MM: SharedMemoryManager<Target = T>> Hash for Shared<T, MM> {
515519
#[inline]
516520
fn hash<H>(&self, state: &mut H)
517521
where
@@ -523,7 +527,7 @@ impl<T, MM: SharedMemoryManager<T>> Hash for Shared<T, MM> {
523527

524528
impl<'a, T: 'static, MM> ToGlibPtr<'a, *mut T> for Shared<T, MM>
525529
where
526-
MM: SharedMemoryManager<T> + 'static,
530+
MM: SharedMemoryManager<Target = T> + 'static,
527531
{
528532
type Storage = PhantomData<&'a Self>;
529533

@@ -541,7 +545,7 @@ where
541545
}
542546
}
543547

544-
impl<T: 'static, MM: SharedMemoryManager<T>> FromGlibPtrNone<*mut T> for Shared<T, MM> {
548+
impl<T: 'static, MM: SharedMemoryManager<Target = T>> FromGlibPtrNone<*mut T> for Shared<T, MM> {
545549
#[inline]
546550
unsafe fn from_glib_none(ptr: *mut T) -> Self {
547551
debug_assert!(!ptr.is_null());
@@ -553,7 +557,7 @@ impl<T: 'static, MM: SharedMemoryManager<T>> FromGlibPtrNone<*mut T> for Shared<
553557
}
554558
}
555559

556-
impl<T: 'static, MM: SharedMemoryManager<T>> FromGlibPtrNone<*const T> for Shared<T, MM> {
560+
impl<T: 'static, MM: SharedMemoryManager<Target = T>> FromGlibPtrNone<*const T> for Shared<T, MM> {
557561
#[inline]
558562
unsafe fn from_glib_none(ptr: *const T) -> Self {
559563
debug_assert!(!ptr.is_null());
@@ -565,7 +569,7 @@ impl<T: 'static, MM: SharedMemoryManager<T>> FromGlibPtrNone<*const T> for Share
565569
}
566570
}
567571

568-
impl<T: 'static, MM: SharedMemoryManager<T>> FromGlibPtrFull<*mut T> for Shared<T, MM> {
572+
impl<T: 'static, MM: SharedMemoryManager<Target = T>> FromGlibPtrFull<*mut T> for Shared<T, MM> {
569573
#[inline]
570574
unsafe fn from_glib_full(ptr: *mut T) -> Self {
571575
debug_assert!(!ptr.is_null());
@@ -576,7 +580,7 @@ impl<T: 'static, MM: SharedMemoryManager<T>> FromGlibPtrFull<*mut T> for Shared<
576580
}
577581
}
578582

579-
impl<T: 'static, MM: SharedMemoryManager<T>> FromGlibPtrBorrow<*mut T> for Shared<T, MM> {
583+
impl<T: 'static, MM: SharedMemoryManager<Target = T>> FromGlibPtrBorrow<*mut T> for Shared<T, MM> {
580584
#[inline]
581585
unsafe fn from_glib_borrow(ptr: *mut T) -> Borrowed<Self> {
582586
debug_assert!(!ptr.is_null());

0 commit comments

Comments
 (0)