Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

83%
+8 −0
Q&A How can I define a method on [&mut T] where T: MyTrait?

I'm trying to make a trait method callable on mutable slices of a type implementing that trait (see main). Rust doesn't like me for it. use core::convert::AsMut; trait A { type B; fn f(m: ...

1 answer  ·  posted 3y ago by wizzwizz4‭  ·  last activity 3y ago by r~~‭

#1: Initial revision by user avatar wizzwizz4‭ · 2020-08-21T15:56:01Z (over 3 years ago)
How can I define a method on [&mut T] where T: MyTrait?
I'm trying to make a trait method callable on mutable slices of a type implementing that trait (see `main`). Rust doesn't like me for it.

```rust
use core::convert::AsMut;

trait A {
    type B;
    fn f(m: &mut [Self], b: Self::B);
}

impl<B, M: AsMut<[impl A<B=B>]>> M {
    fn f(&mut self, b: B) {
        A::f(self.as_mut(), b)
    }
}

struct C (u64);
impl A for C {
    type B = u8;
    fn f(m: &mut [Self], b: u8) {
        for c in m {
            c.0 += b;
        }
    }
}

fn main() {
    let v = vec![C(1), C(2), C(3), C(4)];
    v.f(5);
    assert_eq!(v, [C(6), C(7), C(8), C(9)]);
}
```

This gives me errors I don't understand, which is _unusual_ for Rust:

```text
   Compiling playground v0.0.1 (/playground)
error[E0207]: the type parameter `B` is not constrained by the impl trait, self type, or predicates
 --> src/main.rs:8:6
  |
8 | impl<B, M: AsMut<[impl A<B=B>]>> M {
  |      ^ unconstrained type parameter

error[E0207]: the type parameter `impl A<B = B>` is not constrained by the impl trait, self type, or predicates
 --> src/main.rs:8:19
  |
8 | impl<B, M: AsMut<[impl A<B=B>]>> M {
  |                   ^^^^^^^^^^^ unconstrained type parameter

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0207`.
error: could not compile `playground`.
```

Rewording my code slightly doesn't fix this. Is what I'm trying to do even possible in Rust? If so, how?