#is it a good idea to wrap fn main in a macro that my library provides?

27 messages · Page 1 of 1 (latest)

hallow rivet
#

For example, suppose one day actix makes:

// this being an fn main wrapping macro
actix! {
HttpServer::new(|| App::new().service(index))
.bind(("127.0.0.1", 8080))?
.run()
.await
}

Q) Is it recommended for a library to do this?

karmic fern
#

have you seen #[tokio::main] before?

hallow rivet
karmic fern
#

actually, even actix has a #[actix_web::main]

#

anyway, the point is, "wrapping" main is ok, but I'd prefer an attribute macro so that main still exists

hallow rivet
karmic fern
#

and you should keep in mind that it will conflict with all other libraries that are "wrapping" main

#

so just make sure that your library can be used without wrapping main

hallow rivet
#

yup, my aim in other post was to know how I can import async_std in my library in a way that the user using my library won't have to imoprt async-std but "only" import my library and get those features from that.

#

now, because that didn't work, I am finding out alternative ways

hallow rivet
karmic fern
#

yeah i just looked at the other post

#

i think this is fine, but the problem is someone looking for the program entry point will be looking for a fn main

#

so I'd make sure that this will still be present

hallow rivet
# karmic fern yeah i just looked at the other post

oops, sorry for imposing such a huge problem like this (tagging the deleted message)

I changed the macro right now, is this fine and recommended?:

fn main() {
    rohanasan! {
        println!("Listening at http://localhost:8080");
        serve(init(8080), handle).await;
    }
}

this also works:

fn main() {
    println!("Listening at http://localhost:8080");
    rohanasan! {
        serve(init(8080), handle).await;
    }
}

I believe this wont confuse people

karmic fern
hallow rivet
karmic fern
#

i personally don't think it makes a huge difference, but if you're happy with it, it's fine

hallow rivet
#

Only if there was a way to do:

#[rohanasan]
async fn main(){

}
#

Without letting person using my library import async_std

karmic fern
#

you can, you just have to write your own attribute macro

hallow rivet
#

Yes

hallow rivet
#

Atleast this should look easier

#

crafting a #[rohanasan] macro is a huge challenge in itself, I will end up making my own tokio or async_std

loud flax
#

How much magic does this macro do?

#

Preferably you wouldn't need a macro