diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 236fdb9729c..bbe140b7aba 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -19,10 +19,9 @@ pub trait Conv { fn conv(self) -> Self::Output; } -pub trait ConvWith { - type Ctx; +pub trait ConvWith { type Output; - fn conv_with(self, ctx: &Self::Ctx) -> Self::Output; + fn conv_with(self, ctx: CTX) -> Self::Output; } pub trait TryConvWith { @@ -89,8 +88,7 @@ fn conv(self) -> DiagnosticSeverity { } } -impl ConvWith for CompletionItem { - type Ctx = LineIndex; +impl ConvWith<&'_ LineIndex> for CompletionItem { type Output = ::lsp_types::CompletionItem; fn conv_with(self, ctx: &LineIndex) -> ::lsp_types::CompletionItem { @@ -138,8 +136,7 @@ fn conv_with(self, ctx: &LineIndex) -> ::lsp_types::CompletionItem { } } -impl ConvWith for Position { - type Ctx = LineIndex; +impl ConvWith<&'_ LineIndex> for Position { type Output = TextUnit; fn conv_with(self, line_index: &LineIndex) -> TextUnit { @@ -148,8 +145,7 @@ fn conv_with(self, line_index: &LineIndex) -> TextUnit { } } -impl ConvWith for TextUnit { - type Ctx = LineIndex; +impl ConvWith<&'_ LineIndex> for TextUnit { type Output = Position; fn conv_with(self, line_index: &LineIndex) -> Position { @@ -158,8 +154,7 @@ fn conv_with(self, line_index: &LineIndex) -> Position { } } -impl ConvWith for TextRange { - type Ctx = LineIndex; +impl ConvWith<&'_ LineIndex> for TextRange { type Output = Range; fn conv_with(self, line_index: &LineIndex) -> Range { @@ -167,8 +162,7 @@ fn conv_with(self, line_index: &LineIndex) -> Range { } } -impl ConvWith for Range { - type Ctx = LineIndex; +impl ConvWith<&'_ LineIndex> for Range { type Output = TextRange; fn conv_with(self, line_index: &LineIndex) -> TextRange { @@ -208,8 +202,7 @@ fn conv(self) -> Self::Output { } } -impl ConvWith for TextEdit { - type Ctx = LineIndex; +impl ConvWith<&'_ LineIndex> for TextEdit { type Output = Vec; fn conv_with(self, line_index: &LineIndex) -> Vec { @@ -217,8 +210,7 @@ fn conv_with(self, line_index: &LineIndex) -> Vec { } } -impl ConvWith for &'_ AtomTextEdit { - type Ctx = LineIndex; +impl ConvWith<&'_ LineIndex> for &'_ AtomTextEdit { type Output = lsp_types::TextEdit; fn conv_with(self, line_index: &LineIndex) -> lsp_types::TextEdit { @@ -229,10 +221,10 @@ fn conv_with(self, line_index: &LineIndex) -> lsp_types::TextEdit { } } -impl ConvWith for Option { - type Ctx = ::Ctx; - type Output = Option<::Output>; - fn conv_with(self, ctx: &Self::Ctx) -> Self::Output { +impl, CTX> ConvWith for Option { + type Output = Option; + + fn conv_with(self, ctx: CTX) -> Self::Output { self.map(|x| ConvWith::conv_with(x, ctx)) } } @@ -454,35 +446,34 @@ pub fn to_location( Ok(loc) } -pub trait MapConvWith<'a>: Sized + 'a { - type Ctx; +pub trait MapConvWith: Sized { type Output; - fn map_conv_with(self, ctx: &'a Self::Ctx) -> ConvWithIter<'a, Self, Self::Ctx> { + fn map_conv_with(self, ctx: CTX) -> ConvWithIter { ConvWithIter { iter: self, ctx } } } -impl<'a, I> MapConvWith<'a> for I -where - I: Iterator + 'a, - I::Item: ConvWith, -{ - type Ctx = ::Ctx; - type Output = ::Output; -} - -pub struct ConvWithIter<'a, I, Ctx: 'a> { - iter: I, - ctx: &'a Ctx, -} - -impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx> +impl MapConvWith for I where I: Iterator, - I::Item: ConvWith, + I::Item: ConvWith, { - type Item = ::Output; + type Output = >::Output; +} + +pub struct ConvWithIter { + iter: I, + ctx: CTX, +} + +impl Iterator for ConvWithIter +where + I: Iterator, + I::Item: ConvWith, + CTX: Copy, +{ + type Item = >::Output; fn next(&mut self) -> Option { self.iter.next().map(|item| item.conv_with(self.ctx))