How to finish follow code?

struct ValueWrapper<T> {
   value: T,
}

pub struct Stream<'a> {
    stream: &'a[u8],
    len: usize,
    read: usize,
}

trait StreamShr<T> : Shr<T> {
    fn shr(self, rhs: T) -> Self::Output;
}

impl<'b, 'a, T> Shr<&'b mut ValueWrapper<T>> for &'a mut Stream<'a> where &'a mut Stream<'a> : StreamShr<&'b mut T> {
    type Output = Self;

    fn shr(self, rhs: &'b mut ValueWrapper<T>) -> Self::Output {
        // call self >> & mut rhs.value
    }
}

I want to implement the code above, but had some problems:
(1) exepect code "self >> & mut rhs.value" directly return & mut Stream<'a> , but build follow err:
--> core/src/lib.rs:26:9
|
25 | fn shr(self, rhs: &'b mut ValueWrapper) -> Self::Output {
| ------------ expected &mut Stream<'_> because of return type
26 | self >> & mut rhs.value
| ^^^^^^^^^^^^^^^^^^^^^^^ expected &mut Stream<'_>, found associated type
|
(2) I try to write follow code, it still not working:

self.clone() >> & mut rhs.value;
        self

& mut T don't have method clone or copy、

Ask Help for : how can I solve this problem?

You probably never want a &'a mut MyStruct<'a>:

Then, to be honest I’m not sure to understand exactly what you are trying to achieve here, and implementing the Shr trait for Stream<'a> doesn’t sound right to me. What it means to perform a right shift of a value with the type T on a Stream? I feel Shr is not meant to be implemented like this. Maybe you can clarify exactly what you are trying to do, and we can suggest you a better approach.

That being said, here is one way to get this code to compile without error:

use std::ops::Shr;

struct ValueWrapper<T> {
    value: T,
}

pub struct Stream<'a> {
    stream: &'a [u8],
    len: usize,
    read: usize,
}

trait StreamShr<T>: Shr<T> {}

impl<'a, 'b, T> Shr<&'a mut ValueWrapper<T>> for &'a mut Stream<'b>
where
    Self: StreamShr<&'a mut T>,
{
    type Output = <Self as Shr<&'a mut T>>::Output;

    fn shr(self, rhs: &'a mut ValueWrapper<T>) -> Self::Output {
        self >> (&mut rhs.value)
    }
}
1 Like

It would help if the was complete. I had to add use std::ops::Shr; and self >> & mut rhs.value to make the error.

I do not understand what you are trying to accomplish. StreamShr does not seem to do anything. I do not understand what you think self >> &mut rhs.value is meant to do.

If I guess about your intentions, I can get this to compile:

use std::ops::Shr;

struct ValueWrapper<T> {
   value: T,
}

pub struct Stream<'a> {
    stream: &'a[u8],
    len: usize,
    read: usize,
}

trait StreamShr<T> : Shr<T> {
    fn shr(self, rhs: T) -> Self;
}

impl<'b, 'a, T> Shr<&'b mut ValueWrapper<T>> for &'a mut Stream<'a> where &'a mut Stream<'a> : StreamShr<&'b mut T> {
    type Output = Self;

    fn shr(self, rhs: &'b mut ValueWrapper<T>) -> Self::Output {
        StreamShr::shr(self, &mut rhs.value)
    }
}

But I'm not sure if that's what you even want.

If you are attempting to make C++-style streams using << and >>, maybe do not. Abuse of operators like that is somewhat frowned upon in Rust. It is like making a function "divide_by_two" actually print ":rocket:" to the console.

6 Likes

I believe that's exactly what OP is trying to do (and agre that s/he should not).

Thank you for your answer, You Guss is correct, I'm attempting to parse protocol filed from stream。at first I want to Implementing the Stream parsing framework。

Thank you for your answer, the solution works. :rose:

Is there anything better solution you can recommend to me?

Implement the std::io::{Read, Write} traits instead.

1 Like