Skip to content

Commit

Permalink
Removed deprecated warnings, added ability to add lifetimes to `impl_…
Browse files Browse the repository at this point in the history
…for`
  • Loading branch information
VictorKoenders committed Dec 4, 2021
1 parent 0cedf89 commit 6a7c052
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
25 changes: 21 additions & 4 deletions src/generate/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,30 @@ impl Generator {
ImplFor::new(self, trait_name)
}

/// Generate an `for <'__de> <trait_name> for <target_name>` implementation. See [ImplFor] for more information.
#[deprecated(note = "Should be replace with generic lifetimes")]
pub fn impl_for_with_de_lifetime<'a>(
/// Generate an `for <..lifetimes> <trait_name> for <target_name>` implementation. See [ImplFor] for more information.
///
/// Note:
/// - Lifetimes should _not_ have the leading apostrophe.
/// - The lifetimes passed to this function will automatically depend on any other lifetime this struct or enum may have. Example:
/// - The struct is `struct Foo<'a> {}`
/// - You call `generator.impl_for_with_lifetime("Bar", &["b"])
/// - The code will be `impl<'a, 'b: 'a> Bar<'b> for Foo<'a> {}`
/// - `trait_name` should _not_ have custom lifetimes. These will be added automatically.
///
/// ```no_run
/// # use virtue::prelude::*;
/// # let mut generator: Generator = unsafe { std::mem::zeroed() };
/// generator.impl_for_with_lifetime("Foo", &["a", "b"]);
///
/// // will output:
/// // impl<'a, 'b> Foo<'a, 'b> for StructOrEnum { }
/// ```
pub fn impl_for_with_lifetimes<'a>(
&'a mut self,
trait_name: &str,
lifetimes: &[&str],
) -> Result<ImplFor<'a>, PushParseError> {
ImplFor::new_with_de_lifetime(self, trait_name)
ImplFor::new_with_lifetimes(self, trait_name, lifetimes)
}

/// Consume the contents of this generator. This *must* be called, or else the generator will panic on drop.
Expand Down
21 changes: 15 additions & 6 deletions src/generate/impl_for.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,29 @@ impl<'a> ImplFor<'a> {
Ok(Self { generator, group })
}

#[deprecated(note = "Should be replace with generic lifetimes")]
pub(super) fn new_with_de_lifetime(
pub(super) fn new_with_lifetimes(
generator: &'a mut Generator,
trait_name: &str,
lifetimes: &[&str],
) -> Result<Self, PushParseError> {
let mut builder = StreamBuilder::new();
builder.ident_str("impl");

if let Some(generics) = &generator.generics {
builder.append(generics.impl_generics_with_additional_lifetime("__de"));
builder.append(generics.impl_generics_with_additional_lifetimes(lifetimes));
} else {
builder.punct('<');
builder.lifetime_str("__de");
builder.punct('>');
append_lifetimes(&mut builder, lifetimes);
}

builder.push_parsed(trait_name)?;
append_lifetimes(&mut builder, lifetimes);
builder.ident_str("for");
builder.ident(generator.name.clone());

if let Some(generics) = &generator.generics {
builder.append(generics.type_generics());
}

if let Some(generic_constraints) = &generator.generic_constraints {
builder.append(generic_constraints.where_clause());
}
Expand Down Expand Up @@ -90,3 +91,11 @@ impl Drop for ImplFor<'_> {
.group(Delimiter::Brace, |builder| builder.append(stream))
}
}

fn append_lifetimes(builder: &mut StreamBuilder, lifetimes: &[&str]) {
for (idx, lt) in lifetimes.iter().enumerate() {
builder.punct(if idx == 0 { '<' } else { ',' });
builder.lifetime_str(lt);
}
builder.punct('>');
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub use self::error::Error;
#[cfg(test)]
/// Useful includes
pub mod prelude {
pub use crate::generate::FnSelfArg;
pub use crate::generate::{FnSelfArg, Generator};
pub use crate::parse::{Body, FromAttribute, Parse};
pub use crate::Result;
pub use proc_macro2::*;
Expand All @@ -89,7 +89,7 @@ pub mod prelude {
pub mod prelude {
extern crate proc_macro;

pub use crate::generate::FnSelfArg;
pub use crate::generate::{FnSelfArg, Generator};
pub use crate::parse::{Body, FromAttribute, Parse};
pub use crate::Result;
pub use proc_macro::*;
Expand Down
30 changes: 17 additions & 13 deletions src/parse/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,28 @@ impl Generics {
result
}

#[deprecated(note = "Should take a list of lifetimes")]
pub(crate) fn impl_generics_with_additional_lifetime(&self, lifetime: &str) -> StreamBuilder {
pub(crate) fn impl_generics_with_additional_lifetimes(
&self,
lifetime: &[&str],
) -> StreamBuilder {
assert!(self.has_lifetime());

let mut result = StreamBuilder::new();
result.punct('<');
result.lifetime_str(lifetime);

if self.has_lifetime() {
for (idx, lt) in self.iter().filter_map(|lt| lt.as_lifetime()).enumerate() {
result.punct(if idx == 0 { ':' } else { '+' });
result.lifetime(lt.ident.clone());
for (idx, lt) in lifetime.iter().enumerate() {
result.punct(if idx == 0 { '<' } else { ',' });
result.lifetime_str(lt);

if self.has_lifetime() {
for (idx, lt) in self.iter().filter_map(|lt| lt.as_lifetime()).enumerate() {
result.punct(if idx == 0 { ':' } else { '+' });
result.lifetime(lt.ident.clone());
}
}
}

for generic in self.iter() {
result.punct(',');
generic.append_to_result_with_constraints(&mut result);
for generic in self.iter() {
result.punct(',');
generic.append_to_result_with_constraints(&mut result);
}
}

result.punct('>');
Expand Down

0 comments on commit 6a7c052

Please sign in to comment.