udoprog / genco

A whitespace-aware quasiquoter for beautiful code generation.
Apache License 2.0
181 stars 11 forks source link

consider making `Import` implement `Copy` #40

Open cameronbraid opened 1 year ago

cameronbraid commented 1 year ago

To use an import multiple times you have to manually clone it :


    let map = rust::import("std::collections", "HashMap");

    let tokens: rust::Tokens = quote! {
        fn test() {
            let mut m = $(map.clone())::new();
            m.insert(1u32, 2u32);
            let mut m2 = $(map.clone())::new();
            m2.insert(1u32, 2u32);
        }
    };

Otherwise without the clone :

    let map = rust::import("std::collections", "HashMap");

    let tokens: rust::Tokens = quote! {
        fn test() {
            let mut m = $map::new();
            m.insert(1u32, 2u32);
            let mut m2 = $map::new();
            m2.insert(1u32, 2u32);
        }
    };

you get error :

20 |     let map = rust::import("std::collections", "HashMap");
   |         --- move occurs because `map` has type `genco::lang::rust::Import`, which does not implement the `Copy` trait
...
24 |             let mut m = $map::new();
   |                          --- value moved here
25 |             m.insert(1u32, 2u32);
26 |             let mut m2 = $map::new();
   |                           ^^^ value used here after move
udoprog commented 1 year ago

Makes sense, but probably use some form of implicit cloning since rust::import could produce a value which is heap allocated.

Thanks for the suggestion!