Cannot add-assign `bool` to `bool` (Actix)

Hello.
How to implement add-assign ?

Error:

error[E0277]: cannot add-assign bool to bool
--> src/main.rs:146:41
|
146 | ... dh_addr.do_send(data);
| ^^^^ no implementation for bool += bool
|
= help: the trait std::ops::AddAssign is not implemented for bool
= note: required because of the requirements on the impl of actix::handler::Handler<datahub::Data<bool>> for datahub::DataHub

My Code:

let value = true as bool;
mytActor.do_send(Data::new_out("myID".to_string(), value));

...

#[derive(Debug, Clone)]
pub enum Direction
{
    Out,
    In,
}

impl Default for Direction {
    fn default() -> Self { Direction::In }
}

#[derive(Debug, Clone, Default)]
pub struct Data<T> {
    pub value: T,
    pub id: String,
    pub dir : Direction,
}

impl<T: std::default::Default> Data<T> {
    pub fn new( t: T ) -> Data<T>
    {
        let mut data = Data::default();
        data.value  = t;
        return data;
    }

    pub fn new_out( id : String, t: T ) -> Data<T>
    {
        let mut data = Data::new(t);
        data.dir = Direction::Out;
        data.id = id;
        return data;
    }
}

impl<T: 'static> Message for Data<T> {
    type Result = T;
}

impl AddAssign<bool> for Data<bool>
{
    fn add_assign(&mut self, rhs: bool) {
     self.value = rhs;
    }
}

This code compiles for me (playground). Can you share the code where the error appears? What does the do_send method look like?

1 Like

Actix probably causes compilation errors.

My Repo:

This code compiles if you remove the T: AddAssign<T> requirement in the Handler impls for both DataHub and Driver.

I'm not sure why this requirement was there, but if you intend this code to make use of your AddAssign impl, then the correct bound would be where Data<T>: AddAssign<T>, and you would also need impls for Data<i32> and Data<u32>.

You can't implement AddAssign for bool because it is not a type that is defined by the current crate. (If this were allowed, then you could get conflicting impls from different crates.)

Do I understand correctly?
Will Data work only with T = u8..u32, i8..u32, fp32?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.