Skip to content

Commit

Permalink
Move from venial to syn.
Browse files Browse the repository at this point in the history
  • Loading branch information
raldone01 committed Dec 12, 2024
1 parent 76ac305 commit a6f4365
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 242 deletions.
7 changes: 0 additions & 7 deletions .gitpod.yml

This file was deleted.

29 changes: 16 additions & 13 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@
"cSpell.diagnosticLevel": "Information",
"cSpell.words": [
"castable",
"casted",
"clippy",
"downcastable",
"downcasting",
"downcasts",
"intertrait",
"Kokusnuss",
"mopa",
"onestacked",
"Peekable",
"println",
"proc",
"ptrs",
"punct",
"raldone",
"rustc",
"rustdoc",
"selv",
"struct",
"structs",
"Traitcast",
"traitcastable",
"TraitcastableAny",
"TraitcastableTo",
"ptrs",
"upcasting",
"downcasting",
"casted",
"structs",
"struct",
"println",
"raldone",
"onestacked",
"mopa",
"intertrait",
"rustc",
"clippy"
"vars",
"Zunstable"
]
}
}
33 changes: 17 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,47 @@
[workspace]
resolver = "2"
members = ["trait_cast_impl_rs", "trait_cast_rs"]

[workspace.lints.clippy]
pedantic = "warn"
nursery = "warn"
doc_markdown = "warn"
manual_let_else = "warn"
match_same_arms = "warn"
redundant_closure_for_method_calls = "warn"
redundant_else = "warn"
semicolon_if_nothing_returned = "warn"
type_complexity = "allow"
undocumented_unsafe_blocks = "warn"
unwrap_or_default = "warn"
needless_lifetimes = "allow"
option_if_let_else = "allow"

ptr_as_ptr = "warn"
ptr_cast_constness = "warn"
ref_as_ptr = "warn"

# see: https://github.com/bevyengine/bevy/pull/15375#issuecomment-2366966219
too_long_first_doc_paragraph = "allow"

std_instead_of_core = "warn"
std_instead_of_alloc = "warn"
alloc_instead_of_core = "warn"

[workspace.lints.rust]
nonstandard-style = "warn"
future-incompatible = "warn"
missing_docs = "warn"
unused = "warn"
rust_2018_idioms = "warn"
rust-2024-compatibility = "warn"
array-into-iter = "warn"
bare-trait-objects = "warn"
ellipsis-inclusive-range-patterns = "warn"
non-fmt-panics = "warn"
explicit-outlives-requirements = "warn"
unused-extern-crates = "warn"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(docsrs_dep)'] }
unsafe_code = "deny"
unsafe_op_in_unsafe_fn = "warn"
unused_qualifications = "warn"
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
check-private-items = true
11 changes: 11 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
match_block_trailing_comma = true
tab_spaces = 2
condense_wildcard_suffixes = false
newline_style = "Unix"

# The options below are unstable
unstable_features = true
imports_granularity = "Crate"
normalize_comments = true

# these options seem poorly implemented and cause churn, so, try to avoid them
# wrap_comments = true
# comment_width = 100
16 changes: 14 additions & 2 deletions trait_cast_impl_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,17 @@ proc-macro = true

[dependencies]
proc-macro2 = { version = "1.0", features = ["nightly"] }
venial = "0.5.0"
quote = "1.0.21"
syn = { version = "2.0", default-features = false, features = [
"full",
"parsing",
"printing",
"proc-macro",
] }
quote = "1.0"

[lints]
workspace = true

[package.metadata.docs.rs]
rustdoc-args = ["-Zunstable-options", "--generate-link-to-definition"]
all-features = true
72 changes: 51 additions & 21 deletions trait_cast_impl_rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
#![deny(missing_docs)]
#![warn(clippy::undocumented_unsafe_blocks, clippy::pedantic, clippy::nursery)]
//! Proc-macro automating the implementation of `trait_cast_rs::TraitcastableAny`.
//!
//! See `make_trait_castable` for more details.
use proc_macro::TokenStream as TokenStream1;
use proc_macro2::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use venial::{parse_declaration, Declaration, Error};
use syn::{
parse::{self, Parse, ParseStream},
parse_macro_input,
punctuated::Punctuated,
Error, ItemEnum, ItemStruct, Token, TypePath,
};

/// Parses a list of TypePaths separated by commas.
struct TraitCastTargets {
vars: Vec<TypePath>,
}

impl Parse for TraitCastTargets {
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
let vars: Vec<TypePath> = Punctuated::<TypePath, Token![,]>::parse_terminated(input)?
.into_iter()
.collect();
Ok(TraitCastTargets { vars: vars })
}
}

impl quote::ToTokens for TraitCastTargets {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let vars = &self.vars;
tokens.extend(quote!(#(#vars),*));
}
}

/// Attribute macro implementing `TraitcastableAny` for a struct, enum or union.
///
Expand Down Expand Up @@ -42,26 +66,32 @@ use venial::{parse_declaration, Declaration, Error};
/// ```
#[proc_macro_attribute]
pub fn make_trait_castable(args: TokenStream1, input: TokenStream1) -> TokenStream1 {
let args = TokenStream::from(args);
let input = match parse_declaration(input.into()) {
Ok(Declaration::Function(fun)) => {
return Error::new_at_span(
fun.name.span(),
"Can not implement `Traitcast` for functions, expected a struct, enum or union definition",
)
.to_compile_error()
.into();
},
Ok(input) => input,
Err(error) => {
return error.to_compile_error().into();
},
};
let item_name = input.name();
// Convert the input to a TokenStream2
let input = TokenStream2::from(input);

let trait_cast_targets = parse_macro_input!(args as TraitCastTargets);

// First, try to parse the input as a struct
let input_struct = syn::parse2::<ItemStruct>(input.clone());
let mut source_ident = input_struct.map(|item_struct| item_struct.ident);

// Maybe it's an enum
if source_ident.is_err() {
let input_enum = syn::parse2::<ItemEnum>(input.clone());
source_ident = input_enum.map(|item_enum| item_enum.ident);
}

if let Err(err) = source_ident {
let mut custom_error_message = Error::new(err.span(), "Expected a struct or enum");
custom_error_message.combine(err);
return custom_error_message.to_compile_error().into();
}

let source_ident = source_ident.unwrap();

TokenStream1::from(quote!(
#input
::trait_cast_rs::make_trait_castable_decl! {
#item_name => (#args)
#source_ident => (#trait_cast_targets)
}))
}
11 changes: 9 additions & 2 deletions trait_cast_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ const_sort = ["dep:const_sort_rs"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
trait_cast_impl_rs = { path = "../trait_cast_impl_rs", version = "0.2.2" }
const_sort_rs = { version = "0.3.3", optional = true }
trait_cast_impl_rs = { path = "../trait_cast_impl_rs" }
const_sort_rs = { version = "0.3", optional = true }

[lints]
workspace = true

[package.metadata.docs.rs]
rustdoc-args = ["-Zunstable-options", "--generate-link-to-definition"]
all-features = true
4 changes: 4 additions & 0 deletions trait_cast_rs/examples/manual.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#![expect(
unsafe_code,
reason = "Manual traitcast implementations require unsafe code."
)]
#![cfg_attr(feature = "min_specialization", feature(min_specialization))]
#![feature(trait_upcasting)]
#![allow(incomplete_features)]
Expand Down
4 changes: 4 additions & 0 deletions trait_cast_rs/examples/manual_gen_struct.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#![expect(
unsafe_code,
reason = "Manual traitcast implementations require unsafe code."
)]
#![cfg_attr(feature = "min_specialization", feature(min_specialization))]
#![feature(ptr_metadata)]

Expand Down
Loading

0 comments on commit a6f4365

Please sign in to comment.