Skip to content

Creating abstractions over Pwm trait is not ideal #244

Closed
@amiraeva

Description

@amiraeva

I'm working on a project in which I've defined an ActuatorMotor which wraps a Pwm channel on my device. As I'm refactoring to make ActuatorMotor generic over the specific Pwm hardware, I've found it annoying that all Pwm functions take Pwm::Channel by move.

Here's an example:

struct ActuatorMotor<P: Pwm> {
    pwm: P,
    channel: P::Channel,
}

impl<P: Pwm> ActuatorMotor<P>
{
    fn set_velocity(&mut self, v: Velocity) {
        // do stuff with velocity or whatever to get duty
        let duty = todo!();
        self.pwm.set_duty(self.channel, duty);
        // error: can't move out of self.channel
    }
}

A workaround that seems to work for now is to make channel store a closure which spits out instances of Pwm::Channel i.e. Fn() -> P::Channel. At a first glance, requiring a Clone and/or Copy bound on Pwm::Channel would solve my problem, or altering the method signatures in Pwm to instead take &mut Pwm::Channel instead of by move.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions