remove unneded From(..) impl

This commit is contained in:
Aleksey Kladov 2019-06-11 18:07:42 +03:00
parent 0dcaded439
commit 26753f0e49
4 changed files with 21 additions and 37 deletions

View file

@ -11,7 +11,7 @@ use ra_syntax::{
use crate::{
Name, AsName, Struct, Union, Enum, EnumVariant, Crate, AstDatabase,
HirDatabase, HirFileId, StructField, FieldSource, Source, HasSource,
HirDatabase, StructField, FieldSource, Source, HasSource,
type_ref::TypeRef, DefDatabase,
};
@ -201,10 +201,7 @@ impl VariantDef {
}
impl StructField {
pub(crate) fn source_impl(
&self,
db: &(impl DefDatabase + AstDatabase),
) -> (HirFileId, FieldSource) {
pub(crate) fn source_impl(&self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> {
let var_data = self.parent.variant_data(db);
let fields = var_data.fields().unwrap();
let ss;
@ -229,12 +226,12 @@ impl StructField {
}
ast::StructKind::Unit => Vec::new(),
};
let field = field_sources
let ast = field_sources
.into_iter()
.zip(fields.iter())
.find(|(_syntax, (id, _))| *id == self.id)
.unwrap()
.0;
(file_id, field)
Source { file_id, ast }
}
}

View file

@ -11,12 +11,6 @@ pub struct Source<T> {
pub ast: T,
}
impl<T> From<(HirFileId, T)> for Source<T> {
fn from((file_id, ast): (HirFileId, T)) -> Self {
Source { file_id, ast }
}
}
pub trait HasSource {
type Ast;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>;
@ -30,9 +24,9 @@ impl Module {
let def_map = db.crate_def_map(self.krate);
let decl_id = def_map[self.module_id].declaration;
let file_id = def_map[self.module_id].definition;
let module_source = ModuleSource::new(db, file_id, decl_id);
let ast = ModuleSource::new(db, file_id, decl_id);
let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
(file_id, module_source).into()
Source { file_id, ast }
}
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
@ -44,32 +38,32 @@ impl Module {
let def_map = db.crate_def_map(self.krate);
let decl = def_map[self.module_id].declaration?;
let ast = decl.to_node(db);
Some((decl.file_id(), ast).into())
Some(Source { file_id: decl.file_id(), ast })
}
}
impl HasSource for StructField {
type Ast = FieldSource;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> {
self.source_impl(db).into()
self.source_impl(db)
}
}
impl HasSource for Struct {
type Ast = TreeArc<ast::StructDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
self.id.source(db).into()
self.id.source(db)
}
}
impl HasSource for Union {
type Ast = TreeArc<ast::StructDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> {
self.id.source(db).into()
self.id.source(db)
}
}
impl HasSource for Enum {
type Ast = TreeArc<ast::EnumDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> {
self.id.source(db).into()
self.id.source(db)
}
}
impl HasSource for EnumVariant {
@ -82,39 +76,39 @@ impl HasSource for Function {
type Ast = TreeArc<ast::FnDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> {
self.id.source(db).into()
self.id.source(db)
}
}
impl HasSource for Const {
type Ast = TreeArc<ast::ConstDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ConstDef>> {
self.id.source(db).into()
self.id.source(db)
}
}
impl HasSource for Static {
type Ast = TreeArc<ast::StaticDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StaticDef>> {
self.id.source(db).into()
self.id.source(db)
}
}
impl HasSource for Trait {
type Ast = TreeArc<ast::TraitDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TraitDef>> {
self.id.source(db).into()
self.id.source(db)
}
}
impl HasSource for TypeAlias {
type Ast = TreeArc<ast::TypeAliasDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TypeAliasDef>> {
self.id.source(db).into()
self.id.source(db)
}
}
impl HasSource for MacroDef {
type Ast = TreeArc<ast::MacroCall>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::MacroCall>> {
(self.id.0.file_id(), self.id.0.to_node(db)).into()
Source { file_id: self.id.0.file_id(), ast: self.id.0.to_node(db) }
}
}

View file

@ -9,7 +9,7 @@ use ra_prof::profile;
use mbe::MacroRules;
use crate::{
Module, DefDatabase, AstId, FileAstId, AstDatabase,
Module, DefDatabase, AstId, FileAstId, AstDatabase, Source,
};
/// hir makes heavy use of ids: integer (u32) handlers to various things. You
@ -265,10 +265,10 @@ pub(crate) trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
let loc = ItemLoc { module: ctx.module, ast_id: ast_id.with_file_id(ctx.file_id) };
Self::intern(ctx.db, loc)
}
fn source(self, db: &(impl AstDatabase + DefDatabase)) -> (HirFileId, TreeArc<N>) {
fn source(self, db: &(impl AstDatabase + DefDatabase)) -> Source<TreeArc<N>> {
let loc = self.lookup_intern(db);
let ast = loc.ast_id.to_node(db);
(loc.ast_id.file_id(), ast)
Source { file_id: loc.ast_id.file_id(), ast }
}
fn module(self, db: &impl DefDatabase) -> Module {
let loc = self.lookup_intern(db);

View file

@ -49,7 +49,7 @@ impl HasSource for ImplBlock {
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> {
let source_map = db.impls_in_module_with_source_map(self.module).1;
let src = self.module.definition_source(db);
(src.file_id, source_map.get(&src.ast, self.impl_id)).into()
Source { file_id: src.file_id, ast: source_map.get(&src.ast, self.impl_id) }
}
}
@ -66,13 +66,6 @@ impl ImplBlock {
ImplBlock { module, impl_id }
}
/// Returns the syntax of the impl block
pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> {
let source_map = db.impls_in_module_with_source_map(self.module).1;
let src = self.module.definition_source(db);
(src.file_id, source_map.get(&src.ast, self.impl_id)).into()
}
pub fn id(&self) -> ImplId {
self.impl_id
}