无法实现进入通用结构的特征

时间:2016-08-27 23:19:07

标签: generics type-conversion rust

我在Rust中为泛型结构实现Into特性时遇到了麻烦。我想要做的简化版本如下:

struct Wrapper<T> {
    value: T
}

impl<T> Into<T> for Wrapper<T> {
    fn into(self) -> T {
        self.value
    }
}

当我尝试编译时,出现以下错误:

error: conflicting implementations of trait `std::convert::Into<_>` for type `Wrapper<_>`: [--explain E0119]
 --> <anon>:5:1
  |>
5 |> impl<T> Into<T> for Wrapper<T> {
  |> ^
note: conflicting implementation in crate `core`

我得到的印象是问题是标准库中的以下实现:

impl<T, U> Into<T> for U where T: From<U>

由于T 可能实施From<Wrapper<T>>,因此可能是一个冲突的实现。有没有解决这个问题的方法?例如,有没有办法让impl<T>块将T限制为不实现From<Wrapper<T>>的类型?

1 个答案:

答案 0 :(得分:3)

实际上......没有办法将此限制为#!/usr/bin/python # -*- coding: utf-8 -*- import sys from PySide.QtCore import * from PySide.QtGui import * import webbrowser class Window(QWidget): def __init__(self, windowTitle, layout): super().__init__() self.resize(800,500) self.setWindowTitle(windowTitle) self.setLayout(layout) class TextField(QTextEdit): def __init__(self, tooltip, layout): super().__init__() self.setToolTip(tooltip) layout.addWidget(self) class Button(QPushButton): def __init__(self, text, layout): super().__init__() self.setText(text) layout.addWidget(self) class Label(QLabel): def __init__(self, text, layout): super().__init__() self.setText(text) layout.addWidget(self) class Table(QTableWidget): def __init__(self, layout): super().__init__() self.cellDoubleClicked.connect(self.slotItemDoubleClicked) layout.addWidget(self) def slotItemDoubleClicked(self,row,col): if col == 0 or col == 1: webbrowser.open(self.item(row, 1).text()) class Dialog(QDialog): def __init__(self, flags, layout): super().__init__() self.setWindowFlags(flags) self.resize(800,500) self.setLayout(popLayout) #Layouts mainLayout = QVBoxLayout() subLayout = QHBoxLayout() subLayout2 = QHBoxLayout() mainLayout.addLayout(subLayout) mainLayout.addLayout(subLayout2) popLayout = QHBoxLayout() #Main tradeApp = QApplication(sys.argv) textedit = TextField('bla',subLayout) textedit2 = TextField('bla2',subLayout) label = Label('Hover over input fields for instructions.', subLayout2) button = Button('click me', subLayout2) label2 = Label('Hover over input fields for instructions.', subLayout2) def doStuff(): gameResults = {'doom' : '111232', 'quake' : '355324'} if len(gameResults) > 0: popup = Dialog((Qt.WindowSystemMenuHint | Qt.WindowTitleHint), popLayout) table = Table(popLayout) table.setRowCount(len(gameResults)) table.setColumnCount(2); table.setHorizontalHeaderItem(0, QTableWidgetItem("Game")) table.setHorizontalHeaderItem(1, QTableWidgetItem("URL")) for index, game in enumerate(sorted(gameResults)): table.setItem(index,0,QTableWidgetItem(game)) table.item(index,0).setFlags( Qt.ItemIsSelectable | Qt.ItemIsEnabled ) table.setItem(index,1,QTableWidgetItem('http://store.steampowered.com/app/'+gameResults[game]+'/')) table.item(index,1).setFlags( Qt.ItemIsSelectable | Qt.ItemIsEnabled ) table.resizeColumnsToContents() popup.exec_() else: msgBox = QMessageBox() msgBox.setText("No results.") msgBox.exec_() button.clicked.connect(doStuff) tradeWindow = Window('Tradefinder', mainLayout) tradeWindow.show() tradeApp.exec_() 未实施T的类型。 Rust没有“否定”条款。

通常,实施From<Wrapper<T>>的方法只是实现Into并免费获取From 。但是,在您的情况下,实现将是:

Into

这会遇到孤儿规则。您不得对所有impl<T> From<Wrapper<T>> for T { fn from(w: Wrapper<T>) -> T { w.value } } 实施From

可能有一招,但我在这里看不到它。