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

Implement Copy trait #19

Closed
EdgardoArriagada opened this issue Feb 28, 2023 · 1 comment
Closed

Implement Copy trait #19

EdgardoArriagada opened this issue Feb 28, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@EdgardoArriagada
Copy link

EdgardoArriagada commented Feb 28, 2023

Is your feature request related to a problem? Please describe.

Cannot implement Facade pattern because there is no implementation of the Copy trait 🤕

Describe the solution you'd like

implement copy/clone (I'm new to rust, please forgive me if it doesn't makes sense 🙏 )

Describe alternatives you've considered

using spinners lib for facade (it worked ✅ )

Additional context

use spinoff::{spinners, Color, Spinner as LibSpinner};

pub struct Spinner {
    msg: String,
    sp: LibSpinner,
}

impl Spinner {
    pub fn new(msg: &String) -> Self {
        Self {
            msg: msg.clone(),
            sp: create_standard_spinner(&msg),
        }
    }

    pub fn success(&mut self) {
        self.sp.success(&self.msg);
    }
}

fn create_standard_spinner(msg: &String) -> LibSpinner {
    LibSpinner::new(spinners::Dots2, msg.clone(), Color::Blue)
}

pub fn create_spinner(msg: &String) -> Spinner {
    Spinner::new(msg)
}

but it results in the following error

error[E0507]: cannot move out of `self.sp` which is behind a mutable reference
  --> shared/commands-helper/src/lib.rs:23:13
   |
23 |             self.sp.success(&self.msg);
   |             ^^^^^^^ move occurs because `self.sp` has type `spinoff::Spinner`, which does not implement the `Copy` trait

For more information about this error, try `rustc --explain E0507`.
                 ^^^^
@ad4mx
Copy link
Owner

ad4mx commented Feb 28, 2023

Hi, this will not be possible, since the struct every spinner is built on, SpinnerFrames, has a field with a Vec<&'static str>, so I can't implement the Copy trait on it. You can clone it however.

#[derive(Debug, Clone)]
pub struct SpinnerFrames {
    pub frames: Vec<&'static str>,
    pub interval: u16,
}

@ad4mx ad4mx closed this as completed Feb 28, 2023
@ad4mx ad4mx added the enhancement New feature or request label Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants