Skip to content

Commit

Permalink
Merge pull request #355 from koto-lang/generic-derive-macros
Browse files Browse the repository at this point in the history
Add support for generic types in the derive macros
  • Loading branch information
irh authored Sep 23, 2024
2 parents e9255a0 + 8338de6 commit bd3f8cf
Show file tree
Hide file tree
Showing 17 changed files with 444 additions and 140 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ The Koto project adheres to

- `tuple.sort_copy` now supports sorting with a key function, like `list.sort`.

#### API

- Improved support for generic `KotoObject` types
- The macros in `koto_derive` have been updated to support generics.
- `KotoField` has been added to reduce boilerplate when using the derive
macros.

### Changed

#### Language
Expand Down
1 change: 0 additions & 1 deletion Cargo.lock

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

6 changes: 6 additions & 0 deletions crates/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ keywords = ["scripting", "language", "koto"]
[lib]
proc-macro = true

[features]
default = ["arc"]
# One memory management scheme can be enabled at a time
arc = []
rc = []

[dependencies]
proc-macro2 = { workspace = true }
quote = { workspace = true }
Expand Down
20 changes: 17 additions & 3 deletions crates/derive/src/koto_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,32 @@ use syn::{parse_macro_input, DeriveInput};

pub(crate) fn derive_koto_copy(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let attributes = koto_derive_attributes(&input.attrs);
let name = input.ident;
let (impl_generics, ty_generics, generic_where_clause) = input.generics.split_for_impl();

let attributes = koto_derive_attributes(&input.attrs);
let (required_trait, copy_impl) = if attributes.use_copy {
(quote! {Copy}, quote! {(*self).into()})
} else {
(quote! {Clone}, quote! {self.clone().into()})
};

let name = input.ident;
let object_where_clause = quote! { #name #ty_generics: KotoObject + #required_trait };
let where_clause = if let Some(generic_where_clause) = generic_where_clause {
if generic_where_clause.predicates.trailing_punct() {
quote! { #generic_where_clause #object_where_clause }
} else {
quote! { #generic_where_clause, #object_where_clause }
}
} else {
quote! { where #object_where_clause }
};

let result = quote! {
#[automatically_derived]
impl KotoCopy for #name where #name: #required_trait {
impl #impl_generics KotoCopy for #name #ty_generics
#where_clause
{
fn copy(&self) -> KObject {
#copy_impl
}
Expand Down
Loading

0 comments on commit bd3f8cf

Please sign in to comment.