Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rust): cargo fmt and refactor into two workspace with two crates #164

Merged
merged 9 commits into from
Nov 1, 2021

Conversation

willemneal
Copy link
Contributor

@willemneal willemneal commented Oct 26, 2021

This updates the files with the standard rust formatter. If this isn't welcomed, I understand.

If it is welcomed, would you like to add a CI check or a git hook?

@cohix
Copy link
Contributor

cohix commented Oct 26, 2021

Thanks for this @willemneal! Does it always use spaces for formatting? It's odd when it introduces spaces when the rest of the repo is using tabs.

I'm not personally a fan of how the formatter likes to put function parameters on new lines like that, but I'm not against it if that's the recommended way :P

@cohix
Copy link
Contributor

cohix commented Oct 26, 2021

Looks like some conflicts have come up as well

@willemneal
Copy link
Contributor Author

Switched to use tabs and since you had added the proc_macro, I moved the core into its own folder. This makes it clear what the top level exports are.

I also added a lop level Cargo.toml as IDE tools like top level config. Let me know if this is too big of a PR.

@@ -1,5 +1,5 @@
[package]
name = "suborbital"
name = "suborbital-core"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't think we can change this, since then the package name on Cargo would change, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this would work since the parent crate still had the name. However, after some digging it turns out you can't publish a crate with local dependencies.

See: rust-lang/cargo#1565 (comment)

So I reverted to using two crates, that will need to be published separately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you refactor that anyway ... I always wanted to ask why there is additional folder depth in the rust directory?
We have:

  • .../api/assemblyscript/code
  • .../api/rust/suborbital/code
  • .../api/swift/code
  • .../api/tinygo/code

Would it be possible to remove the extra dir to make it more coherent.
It should not make a difference regarding the crate. The crates name is defined in the Cargo.toml.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we can get rid of the extra depth, I did that when I didn't fully understand how Rust crates were organized :)

Also, I don't think I want to have a fully separate crate for the macro? Is there any reason it can't just be a module inside the existing crate?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derive Macros have to be defined in their own crate as described in the docs:

Procedural macros must be defined in a crate with the crate type of proc-macro.

That seems to be how the crate system works in that regard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately a proc_macro must be inside its own crate. And as stated above you have to publish local dependencies if you want to use them in your published your crate. So both need to be published. I've renamed the macro to a non-generic name to prepare for this.

So in this case it's unavoidable to have two crates. However, Rust projects are often broken up into crates. In fact, I would suggest a further refactoring to pull out all extern definitions to their own crate like is done here: https://github.com/near/near-sdk-rs/blob/master/sys/src/lib.rs

Personally I find it very useful for a Wasm project with non-standard imports to collect them all in one place. Then have the associated wrappers depend on that.

The other win for using workspaces is that you can use private crates. This lets you provide an example and test together.

Lastly, separating your API and types from your implementation is another benefit, although, in this case it's definitely overkill.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An own crate for the extern stuff sounds, I like the separation that creates.

@willemneal willemneal requested a review from cohix October 27, 2021 01:22
@cohix
Copy link
Contributor

cohix commented Oct 28, 2021

Other than the last open thread/question, this looks great! I'll approve once we have that answered.

@willemneal
Copy link
Contributor Author

@cohix @FlrnFrmm It should be good to go after a rebase.

One thing I noticed was for some files 120 still seemed too small for function calls. If after viewing the changes you agree I can edit the config.

@willemneal willemneal changed the title chore(rust): cargo fmt chore(rust): cargo fmt and refactor into two workspace with two crates Oct 28, 2021
@FlrnFrmm
Copy link
Contributor

@cohix @FlrnFrmm It should be good to go after a rebase.

One thing I noticed was for some files 120 still seemed too small for function calls. If after viewing the changes you agree I can edit the config.

Like I said I'm happy with a bigger value, but I'm also just another contributor. From my side feel free to increase the value 👍

api/rust/core/src/lib.rs Outdated Show resolved Hide resolved
@willemneal
Copy link
Contributor Author

@cohix It now includes the option runnable PR's updates.

@willemneal
Copy link
Contributor Author

Also moved re-export to make it clearer.

@cohix cohix merged commit 736574b into suborbital:main Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants