如何从实现中返回特征,以便可以链接调用?

时间:2019-01-17 16:12:26

标签: rust traits

我具有几个实现的特征,我想返回该对象以便链接调用。

pub trait RequestInfo {
    fn logged_in(&self) -> bool;
    fn put(&mut self, string: String) -> RequestInfo {
        self
    }
}

struct LoggedOut {}
impl LoggedOut {
    fn new() -> Box<RequestInfo> {
        Box::new(LoggedOut {})
    }
}
impl RequestInfo for LoggedOut {
    fn logged_in(&self) -> bool {
        false
    }
}

struct LoggedIn {
    output: Vec<String>,
}
impl LoggedIn {
    fn new() -> Box<RequestInfo> {
        Box::new(LoggedIn { output: Vec::new() })
    }
}
impl RequestInfo for LoggedIn {
    fn logged_in(&self) -> bool {
        true
    }
    fn put(&mut self, string: String) -> impl RequestInfo {
        self.output.push(string);
        self
    }
}

fn main() {
    let mut info = LoggedIn::new();
    info.put("abc".to_string()).put("def".to_string());
}

我收到错误消息:

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
  --> src/main.rs:32:42
   |
32 |     fn put(&mut self, string: String) -> impl RequestInfo {
   |                                          ^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
3 |     fn put(&mut self, string: String) -> RequestInfo {
  |                                          ----------- expected `(dyn RequestInfo + 'static)` because of return type
4 |         self
  |         ^^^^ expected trait RequestInfo, found &mut Self
  |
  = note: expected type `(dyn RequestInfo + 'static)`
             found type `&mut Self`

error[E0277]: the size for values of type `(dyn RequestInfo + 'static)` cannot be known at compilation time
 --> src/main.rs:3:42
  |
3 |     fn put(&mut self, string: String) -> RequestInfo {
  |                                          ^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `(dyn RequestInfo + 'static)`
  = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: the return type of a function must have a statically known size

唯一可行的方法是Box自我,就像我在new()函数中所做的那样,但是我不想通过链接来创建任何额外的代码……这实际上只是还是方便。

返回&mut Self并使用Box<impl RequestInfo>几乎可以工作...。除了我有一个返回LoggedIn对象或LoggedOut对象的函数外,因此这里进行了修订代码:

pub trait RequestInfo {
    fn logged_in(&self) -> bool;
    fn put(&mut self, string: String) -> &mut Self {
        self
    }
}

struct LoggedOut {}
impl LoggedOut {
    fn new() -> Box<impl RequestInfo> {
        Box::new(LoggedOut {})
    }
}
impl RequestInfo for LoggedOut {
    fn logged_in(&self) -> bool {
        false
    }
}

struct LoggedIn {
    output: Vec<String>,
}
impl LoggedIn {
    fn new() -> Box<impl RequestInfo> {
        Box::new(LoggedIn { output: Vec::new() })
    }
}
impl RequestInfo for LoggedIn {
    fn logged_in(&self) -> bool {
        true
    }
    fn put(&mut self, string: String) -> &mut Self {
        self.output.push(string);
        self
    }
}

fn get(flag: bool) -> Box<impl RequestInfo> {
    if flag {
        return LoggedIn::new();
    }
    LoggedOut::new()
}

fn main() {
    let mut info = get(true);
    info.put("abc".to_string()).put("def".to_string());
}

它给出了以下错误(在函数的早期它返回了一个LoggedIn对象):

error[E0308]: mismatched types
  --> src/main.rs:42:5
   |
42 |     LoggedOut::new()
   |     ^^^^^^^^^^^^^^^^ expected opaque type, found a different opaque type
   |
   = note: expected type `std::boxed::Box<impl RequestInfo>` (opaque type)
              found type `std::boxed::Box<impl RequestInfo>` (opaque type)

2 个答案:

答案 0 :(得分:2)

现在进入最后一步:您将注意到带有返回Self can not be used as trait objects的方法的特征。该链接可以解决您的问题:用where Self: Sized标记该方法,这样它就不会出现在您的trait对象上。但是,那么您就无法使用特征对象进行链接。这可以通过在Box<dyn RequestInfo>上实现方法来解决,该方法不是特征对象,而是Box。因此,将所有这些放在一起:

pub trait RequestInfo {
    fn logged_in(&self) -> bool;
    fn put(&mut self, string: String) -> &mut Self
    where Self: Sized {
        self.put_internal(string);
        self
    }
    fn put_internal(&mut self, string: String) {}
}
impl RequestInfo for Box<dyn RequestInfo> {
    fn logged_in(&self) -> bool {
        self.as_ref().logged_in()
    }
}

struct LoggedOut {}
impl RequestInfo for LoggedOut {
    fn logged_in(&self) -> bool {false}
}

struct LoggedIn {output: Vec<String>}
impl LoggedIn {
    fn new() -> LoggedIn {
        LoggedIn { output: Vec::new() }
    }
}
impl RequestInfo for LoggedIn {
    fn logged_in(&self) -> bool {true}
    fn put_internal(&mut self, string: String) {
        self.output.push(string);
   }
}
fn get(flag: bool) -> Box<dyn RequestInfo> {
    if flag {Box::new(LoggedIn::new())} else {Box::new(LoggedOut{})}
}

fn main() {
    let mut info = get(true);
    info.put("abc".to_string()).put("def".to_string());
}

您需要确定所有这些是否值得进行一些链接。

答案 1 :(得分:0)

I gave this some more thought and you can disregard my previous answer. That solution is unnecessarily complex. I got way to focused on returning &mut Self from put, even though that wasn't asked for at all. You can just return &mut RequestInfo from your put method and you're fine. The only price you pay is that you can't have a default implementation for put anymore.

pub trait RequestInfo {
    fn put(self: &mut Self, string: String) -> &mut dyn RequestInfo;
}

struct LoggedOut {}
impl RequestInfo for LoggedOut {
    fn put(self: &mut Self, string: String) -> &mut dyn RequestInfo {
        self
    }
}

struct LoggedIn {
    output: Vec<String>,
}
impl LoggedIn {
    fn new() -> LoggedIn {
        LoggedIn { output: Vec::new() }
    }
}
impl RequestInfo for LoggedIn {
     fn put(self: &mut Self, string: String) -> &mut dyn RequestInfo {
        self.output.push(string);
        self
    }
}

fn get(flag: bool) -> Box<dyn RequestInfo> {
    if flag {Box::new(LoggedIn::new())} else {Box::new(LoggedOut{})}
}


fn main() {
    let mut info = get(false);
    info.put("abc".to_string()).put("def".to_string());
}