是否可以提供可链接特征方法的默认实现?
Is it possible to provide default implementation of chainable trait methods?
目标是拥有类似于 (playground) 的内容:
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
fn forwardable_append_attribute(mut self, new_value: &str) -> Self {
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}
这给出了错误:
error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> src/main.rs:3:37
|
3 | fn forwardable_append_attribute(mut self, new_value: &str) -> Self {
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: unsized locals are gated as an unstable feature
help: consider further restricting `Self`
|
3 | fn forwardable_append_attribute(mut self, new_value: &str) -> Self where Self: std::marker::Sized {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: function arguments must have a statically known size, borrowed types always have a known size
|
3 | fn forwardable_append_attribute(&mut self, new_value: &str) -> Self {
| ^
另一种方法是为每个实现特征的对象定义特征方法,但这会在所有子对象之间引入重复 (playground):
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
fn forwardable_append_attribute(self, new_value: &str) -> Self;
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
fn forwardable_append_attribute(mut self, new_value: &str) -> Self {
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}
所以,你可以在方法上添加一个 Self
必须是 Sized
的界限:
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
fn forwardable_append_attribute(mut self, new_value: &str) -> Self
// note this trait bound
where
Self: Sized,
{
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}
请注意,您还可以将 Sized
绑定到特征本身而不是方法,这看起来像 trait T: Sized {...}
。如果特征仅提供可链接方法,则您应该这样做,因此为不能具有可链接方法的类型实现特征是没有意义的。或者,您可以采用可变引用和 return 可变引用,而不是将 self
移动到函数中,从而消除对 Sized
:
的需要
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
// note the &mut self and &mut Self
fn forwardable_append_attribute(&mut self, new_value: &str) -> &mut Self {
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}
目标是拥有类似于 (playground) 的内容:
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
fn forwardable_append_attribute(mut self, new_value: &str) -> Self {
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}
这给出了错误:
error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> src/main.rs:3:37
|
3 | fn forwardable_append_attribute(mut self, new_value: &str) -> Self {
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: unsized locals are gated as an unstable feature
help: consider further restricting `Self`
|
3 | fn forwardable_append_attribute(mut self, new_value: &str) -> Self where Self: std::marker::Sized {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: function arguments must have a statically known size, borrowed types always have a known size
|
3 | fn forwardable_append_attribute(&mut self, new_value: &str) -> Self {
| ^
另一种方法是为每个实现特征的对象定义特征方法,但这会在所有子对象之间引入重复 (playground):
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
fn forwardable_append_attribute(self, new_value: &str) -> Self;
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
fn forwardable_append_attribute(mut self, new_value: &str) -> Self {
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}
Self
必须是 Sized
的界限:
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
fn forwardable_append_attribute(mut self, new_value: &str) -> Self
// note this trait bound
where
Self: Sized,
{
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}
请注意,您还可以将 Sized
绑定到特征本身而不是方法,这看起来像 trait T: Sized {...}
。如果特征仅提供可链接方法,则您应该这样做,因此为不能具有可链接方法的类型实现特征是没有意义的。或者,您可以采用可变引用和 return 可变引用,而不是将 self
移动到函数中,从而消除对 Sized
:
trait T {
fn get_mutable_attribute(&mut self) -> &mut String;
// note the &mut self and &mut Self
fn forwardable_append_attribute(&mut self, new_value: &str) -> &mut Self {
let attribute = self.get_mutable_attribute();
attribute.push_str(new_value);
println!("{}", attribute);
self
}
}
struct S {
attribute: String,
}
impl T for S {
fn get_mutable_attribute(&mut self) -> &mut String {
&mut self.attribute
}
}
fn main() {
let s = S {
attribute: "init".to_string(),
}
.forwardable_append_attribute("new_1")
.forwardable_append_attribute("new_2")
.forwardable_append_attribute("new_3");
}