Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEED HELP | Can I just impl immutable methods for these containers? #213

Open
1216892614 opened this issue Jun 30, 2024 · 0 comments
Open

Comments

@1216892614
Copy link

1216892614 commented Jun 30, 2024

Are there any performance issues that may arise from using the following extension methods?

pub trait AsImVec<A: Clone>: Clone {
    fn push_back(&mut self, value: A);
    fn to_pushed_back(&self, value: A) -> Self {
        let mut new = self.clone();

        new.push_back(value);

        new
    }

    fn push_front(&mut self, value: A);
    fn to_pushed_front(&self, value: A) -> Self {
        let mut new = self.clone();

        new.push_front(value);

        new
    }

    fn sort_by<F: Fn(&A, &A) -> std::cmp::Ordering>(&mut self, cmp: F);
    fn to_sorted_by<F: Fn(&A, &A) -> std::cmp::Ordering>(&self, cmp: F) -> Self {
        let mut new = self.clone();

        new.sort_by(cmp);

        new
    }

    fn insert(&mut self, index: usize, value: A);
    fn to_inserted(&mut self, index: usize, value: A) -> Self {
        let mut new = self.clone();

        new.insert(index, value);

        new
    }
}

impl<A: Clone> AsImVec<A> for im_rc::Vector<A> {
    fn push_back(&mut self, value: A) {
        self.push_back(value)
    }

    fn push_front(&mut self, value: A) {
        self.push_front(value)
    }

    fn sort_by<F: Fn(&A, &A) -> std::cmp::Ordering>(&mut self, cmp: F) {
        self.sort_by(cmp)
    }

    fn insert(&mut self, index: usize, value: A) {
        self.insert(index, value)
    }
}

impl<A: Clone> AsImVec<A> for im::Vector<A> {
    fn push_back(&mut self, value: A) {
        self.push_back(value)
    }

    fn push_front(&mut self, value: A) {
        self.push_front(value)
    }

    fn sort_by<F: Fn(&A, &A) -> std::cmp::Ordering>(&mut self, cmp: F) {
        self.sort_by(cmp)
    }

    fn insert(&mut self, index: usize, value: A) {
        self.insert(index, value)
    }
}

In this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant