]> git.lizzy.rs Git - rust.git/commitdiff
replace usages of `algo::generate` with `iter::successors` from std
authorRobin Freyler <robin.freyler@gmail.com>
Sat, 13 Apr 2019 14:43:49 +0000 (16:43 +0200)
committerRobin Freyler <robin.freyler@gmail.com>
Sat, 13 Apr 2019 14:43:49 +0000 (16:43 +0200)
crates/ra_assists/src/split_import.rs
crates/ra_fmt/src/lib.rs
crates/ra_hir/src/ty/autoderef.rs
crates/ra_syntax/src/algo.rs
crates/ra_syntax/src/ptr.rs
crates/ra_syntax/src/syntax_node.rs

index 4bf1852db5774b28143fc2498238d15cf399c299..57e0efaf208f7a66d261615e0c356820a88c02fc 100644 (file)
@@ -1,8 +1,9 @@
+use std::iter::successors;
+
 use hir::db::HirDatabase;
 use ra_syntax::{
     TextUnit, AstNode, SyntaxKind::COLONCOLON,
     ast,
-    algo::generate,
 };
 
 use crate::{AssistCtx, Assist, AssistId};
@@ -10,7 +11,7 @@
 pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
     let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?;
     let path = ast::Path::cast(colon_colon.parent())?;
-    let top_path = generate(Some(path), |it| it.parent_path()).last()?;
+    let top_path = successors(Some(path), |it| it.parent_path()).last()?;
 
     let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast);
     if use_tree.is_none() {
index 85b7ce2503fa741dcf4e62aa5f302688c92532a2..603be1854a91f9d72955ab7bddbd22f6dc86fcf4 100644 (file)
@@ -1,10 +1,10 @@
 //! This crate provides some utilities for indenting rust code.
 //!
+use std::iter::successors;
 use itertools::Itertools;
 use ra_syntax::{
     SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind,
     ast::{self, AstNode, AstToken},
-    algo::generate,
 };
 
 pub fn reindent(text: &str, indent: &str) -> String {
@@ -29,7 +29,7 @@ pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
 }
 
 fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
-    generate(token.prev_token(), |&token| token.prev_token())
+    successors(token.prev_token(), |&token| token.prev_token())
 }
 
 pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
index ab5f008ef9ba634635afc246cb457d54111709a0..a442a856c0244b7d45d16a76d4c21c009dc7c3f9 100644 (file)
@@ -3,7 +3,7 @@
 //! reference to a type with the field `bar`. This is an approximation of the
 //! logic in rustc (which lives in librustc_typeck/check/autoderef.rs).
 
-use ra_syntax::algo::generate;
+use std::iter::successors;
 
 use crate::HirDatabase;
 use super::Ty;
@@ -11,7 +11,7 @@
 impl Ty {
     /// Iterates over the possible derefs of `ty`.
     pub fn autoderef<'a>(self, db: &'a impl HirDatabase) -> impl Iterator<Item = Ty> + 'a {
-        generate(Some(self), move |ty| ty.autoderef_step(db))
+        successors(Some(self), move |ty| ty.autoderef_step(db))
     }
 
     fn autoderef_step(&self, _db: &impl HirDatabase) -> Option<Ty> {
index 06b45135c92e67ad858a5795344fc50f0e33ceb8..1f68fe467f55250da689c1c3ec3f4bd6e2aeae93 100644 (file)
@@ -46,13 +46,3 @@ fn not_trivia(element: &SyntaxElement) -> bool {
 pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement {
     root.0.covering_node(range).into()
 }
-
-// Replace with `std::iter::successors` in `1.34.0`
-pub fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item = T> {
-    ::itertools::unfold(seed, move |slot| {
-        slot.take().map(|curr| {
-            *slot = step(&curr);
-            curr
-        })
-    })
-}
index d8de1c4c1b535ee9ce554b65e7e3df8d9fb2d2f3..15a8b94cd6df645e3ad9cc6a303c6958b0c71f8b 100644 (file)
@@ -1,8 +1,9 @@
-use std::marker::PhantomData;
-
+use std::{
+    marker::PhantomData,
+    iter::successors,
+};
 use crate::{
     AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange,
-    algo::generate,
 };
 
 /// A pointer to a syntax node inside a file. It can be used to remember a
@@ -19,7 +20,7 @@ pub fn new(node: &SyntaxNode) -> SyntaxNodePtr {
     }
 
     pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode {
-        generate(Some(source_file.syntax()), |&node| {
+        successors(Some(source_file.syntax()), |&node| {
             node.children().find(|it| self.range.is_subrange(&it.range()))
         })
         .find(|it| it.range() == self.range && it.kind() == self.kind)
index 64d884287caf757b6dc7ffadd79224bd9afce9f8..dc2352c76d455f45ce2f9764ad07e3c51bb278f2 100644 (file)
@@ -10,6 +10,7 @@
     fmt::{self, Write},
     any::Any,
     borrow::Borrow,
+    iter::successors,
 };
 
 use ra_parser::ParseError;
@@ -195,7 +196,7 @@ pub fn last_token(&self) -> Option<SyntaxToken> {
     }
 
     pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> {
-        crate::algo::generate(Some(self), |&node| node.parent())
+        successors(Some(self), |&node| node.parent())
     }
 
     pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> {
@@ -213,7 +214,7 @@ pub fn descendants_with_tokens(&self) -> impl Iterator<Item = SyntaxElement> {
     }
 
     pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> {
-        crate::algo::generate(Some(self), move |&node| match direction {
+        successors(Some(self), move |&node| match direction {
             Direction::Next => node.next_sibling(),
             Direction::Prev => node.prev_sibling(),
         })
@@ -224,7 +225,7 @@ pub fn siblings_with_tokens(
         direction: Direction,
     ) -> impl Iterator<Item = SyntaxElement> {
         let me: SyntaxElement = self.into();
-        crate::algo::generate(Some(me), move |el| match direction {
+        successors(Some(me), move |el| match direction {
             Direction::Next => el.next_sibling_or_token(),
             Direction::Prev => el.prev_sibling_or_token(),
         })
@@ -373,7 +374,7 @@ pub fn siblings_with_tokens(
         direction: Direction,
     ) -> impl Iterator<Item = SyntaxElement<'a>> {
         let me: SyntaxElement = (*self).into();
-        crate::algo::generate(Some(me), move |el| match direction {
+        successors(Some(me), move |el| match direction {
             Direction::Next => el.next_sibling_or_token(),
             Direction::Prev => el.prev_sibling_or_token(),
         })