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

Manual body definitions #17

Merged
merged 5 commits into from
Nov 13, 2023
Merged

Manual body definitions #17

merged 5 commits into from
Nov 13, 2023

Conversation

AngusGMorrison
Copy link
Contributor

Refactors how empty body structs are defined, moving the responsibility from lox_gen to lox_core::bodies. This helps us to avoid circular compiler errors where generated files are malformed, but can't be regenerated without commenting out all crate code that depends on their body definitions.

Additionally fixes a bug where nut prec coefficients weren't being converted to radians.

Defining body structs manually rather than relying on generated code
prevents circular compiler errors in the event of badly generated code
that must be deleted, but is depended on by manual trait
implementations, which must compile in order to rerun the generator.
.gitignore Outdated
.idea/
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 finally succeeded in gitignoring the idea files successfully. May it rest in peace. Sorry about the extra PR noise.

Copy link
Member

Choose a reason for hiding this comment

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

Did not really care before. This is the nuclear option which is certainly valid but it seems that JetBrains recommends to keep some of these files under version control: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839-How-to-manage-projects-under-Version-Control-Systems

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair enough. It's a habitual response after years of culling .DS_Store and VSCode artifacts. I'm happy to default it.

pub trait NaifId: Copy {
fn id() -> i32;
const ID: i32;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given the opportunity, I refactored this to use the pattern we've adopted for RotationalElements.

/// Expands to derivations of the fundamental traits every body must implement.
macro_rules! body {
($i:ident, $naif_id:literal) => {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Copy link
Contributor Author

@AngusGMorrison AngusGMorrison Nov 12, 2023

Choose a reason for hiding this comment

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

It occurred to me that none of these structs need to be Clone or Copy, and nor do our trait implementations require these bounds, since empty structs have zero size. Rust effectively considers any operation on them (such as passing them to another function) to be a no-op. These can be removed in a small follow-up to avoid creating more noise here.

More on zero-sized types here: https://doc.rust-lang.org/nomicon/exotic-sizes.html#:~:text=feature%20for%20now.)-,Zero%20Sized%20Types%20(ZSTs),-Rust%20also%20allows

Copy link
Member

Choose a reason for hiding this comment

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

Are you sure about this? I only added the annotations after running into trouble.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me run some more experiments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, the following compiles fine for me (where NaifId is defined without a Copy bound):

#[cfg(test)]
mod nocopy_tests {
    use crate::bodies::NaifId;

    struct SomeBody;
    impl NaifId for SomeBody {
        const ID: i32 = 1;
    }

    #[test]
    fn sends_body() {
        let some_body = SomeBody;
        forwards_body(some_body);
        forwards_body(SomeBody);
    }

    fn forwards_body(body: impl NaifId) {
        receives_body(body);
    }

    fn receives_body(body: impl NaifId) {
        println!("all good")
    }
}

BUT, removing Copy would absolutely cause us problems interacting with stdlib or third-party functions that require a Clone/Copy bound, so better to have it than not. As you were!

@@ -202,53 +411,37 @@ pub trait RotationalElements: Copy {
mod tests {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This diff is super confusing because I moved the manual definition of Jupiter up a few lines, and the definition is huge. In short: all tests are now defined in terms of our manually defined Jupiter, decoupling them from the generated code.

@@ -262,7 +262,12 @@ impl GetPolynomialCoefficients for CoefficientKernel<'_> {
}))
}
// Split the implicit pairs into two index-matched vecs.
Some(coefficients) => Ok(TokenizeableNutPrecCoefficients(unpair(coefficients))),
Some(coefficients) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missed this conversion to radians previously!


fn main() {
println!("Hello LOX");
println!("{}", MercuryBarycenter::id())
println!("{}", MercuryBarycenter::NAIF_ID)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Presumably just ::ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you!

@helgee
Copy link
Member

helgee commented Nov 13, 2023

LGTM 👍

@AngusGMorrison AngusGMorrison merged commit 1edfbd2 into main Nov 13, 2023
20 checks passed
@helgee helgee deleted the manual-body-definitions branch February 29, 2024 10:21
This was referenced Jul 19, 2024
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