Skip to content

Implementation extensibility #5

@cark

Description

@cark

Right now, we're adding bounded functionalities by specifying the bounds inside the RefCounted trait like so:

    fn make_mut(this: &mut Self) -> &mut T
    where
        T: Clone;

While this pattern gives results for right now, it requires access to this code for adding new bounded implementations.

This pattern might be a solution, but it's quite verbose and a bit hairy for the end-user

// RefCountedClone
pub trait RefCountedClone<T: Clone + ?Sized>: RefCounted<T> {
    fn make_mut(this: &mut Self) -> &mut T;
}

impl<T: Clone + ?Sized> RefCountedClone<T> for Rc<T> {
    fn make_mut(this: &mut Self) -> &mut T {
        Self::make_mut(this)
    }
}

fn test_make_mut() {
    fn actual_test<Mark: RefCountFamily>()
    // this is less than ideal
    where
        <Mark as RefCountFamily>::Pointer<i32>: RefCountedClone<i32>,
    {
        let mut data = Mark::Pointer::new(5i32);
        *RefCountedClone::make_mut(&mut data) += 1;
        let mut other_data = Mark::Pointer::clone(&data);
        *RefCountedClone::make_mut(&mut data) += 1;
        // other incantation, same thing
        *Mark::Pointer::make_mut(&mut data) += 1;
        *RefCountedClone::make_mut(&mut other_data) *= 2;
        assert_eq!(*data, 8);
        assert_eq!(*other_data, 12);
    }
    actual_test::<RcMark>();

    fn actual_test2<RC: RefCountedClone<i32>>() {
        let mut data = RC::new(5);
        *RC::make_mut(&mut data) += 1;
        let mut other_data = RC::clone(&data);
        *RC::make_mut(&mut data) += 1;
        *RC::make_mut(&mut data) += 1;
        *RC::make_mut(&mut other_data) *= 2;
        assert_eq!(*data, 8);
        assert_eq!(*other_data, 12);
    }
    actual_test2::<Rc<i32>>();
}

does anything else come to mind ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions