T实现显示的T的From From特性

时间:2018-11-11 00:48:22

标签: rust

我正在尝试实现From<T>,其中T为我的自定义类型Display实现Cell

use std::borrow::Cow;
use std::fmt::{Display, Formatter, Result};

pub struct TableCell<'data> {
    pub data: Cow<'data, str>,
    pub col_span: usize,
    pub pad_content: bool,
}

impl<'data> TableCell<'data> {
    pub fn new<T>(data: T) -> TableCell<'data>
    where
        T: ToString,
    {
        return TableCell {
            data: data.to_string().into(),
            col_span: 1,
            pad_content: true,
        };
    }
}

impl<'data> Display for TableCell<'data> {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "{}", self.data)
    }
}

impl<'data, T> From<T> for TableCell<'data>
where
    T: ToString,
{
    fn from(other: T) -> Self {
        return TableCell::new(other);
    }
}

fn main() {
    println!("Hello, world!");
}

我一直遇到以下错误:

error[E0119]: conflicting implementations of trait `std::convert::From<TableCell<'_>>` for type `TableCell<'_>`:
  --> src/main.rs:29:1
   |
29 | / impl<'data, T> From<T> for TableCell<'data>
30 | | where
31 | |     T: ToString,
32 | | {
...  |
35 | |     }
36 | | }
   | |_^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> std::convert::From<T> for T;

这种事情是否可能,或者我需要为特定类型实现From吗?

0 个答案:

没有答案