From 80a6e614465d8b16cae50f3626c15e912ad3c6f2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 20 Aug 2019 18:33:23 +0300 Subject: make CTX type param instead of assoc type that way, we can implement ConvWith<&'_ CTX> for different lifetimes --- crates/ra_lsp_server/src/conv.rs | 59 +++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 34 deletions(-) (limited to 'crates') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 236fdb972..bbe140b7a 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 @@ impl Conv for Severity { } } -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 @@ impl ConvWith for 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 @@ impl ConvWith for Position { } } -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 @@ impl ConvWith for TextUnit { } } -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 @@ impl ConvWith for TextRange { } } -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 @@ impl Conv for ra_ide_api::FunctionSignature { } } -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 @@ impl ConvWith for TextEdit { } } -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 @@ impl ConvWith for &'_ AtomTextEdit { } } -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 +impl MapConvWith for I where - I: Iterator + 'a, - I::Item: ConvWith, + I: Iterator, + I::Item: ConvWith, { - type Ctx = ::Ctx; - type Output = ::Output; + type Output = >::Output; } -pub struct ConvWithIter<'a, I, Ctx: 'a> { +pub struct ConvWithIter { iter: I, - ctx: &'a Ctx, + ctx: CTX, } -impl<'a, I, Ctx> Iterator for ConvWithIter<'a, I, Ctx> +impl Iterator for ConvWithIter where I: Iterator, - I::Item: ConvWith, + I::Item: ConvWith, + CTX: Copy, { - type Item = ::Output; + type Item = >::Output; fn next(&mut self) -> Option { self.iter.next().map(|item| item.conv_with(self.ctx)) -- cgit v1.2.3