我仍在学习 Rust,并尝试rust-cssparser
重新使用源库中定义的 impl,但得到编译错误,cannot define inherent impl for a type outside of the crate where the type is defined
. 当然,图书馆使用use super::Token;
,但我需要use cssparser::{ Token };
,并且不知道如何解决。https://github.com/servo/rust-cssparser/blob/master/src/serializer.rs#L503-L552
摘抄:
impl<'a> Token<'a> {
/// Categorize a token into a type that determines when `/**/` needs to be inserted
/// between two tokens when serialized next to each other without whitespace in between.
///
/// See the `TokenSerializationType::needs_separator_when_before` method.
pub fn serialization_type(&self) -> TokenSerializationType {
use self::TokenSerializationTypeVariants::*;
TokenSerializationType(match *self {
Token::Ident(_) => Ident,
Token::AtKeyword(_) | Token::Hash(_) | Token::IDHash(_) => AtKeywordOrHash,
...
})
}
}
基于帮助note: define and implement a trait or new type instead
和一些搜索,我在下面尝试了几个变体,但我没有深入了解编译错误的杂草。谢谢你的帮助。
use cssparser::{
Token as Tkn,
// Token
};
// struct Token<'a>(Tkn<'a>); // requires changing lots of refs
type Token<'a> = Tkn<'a>;