Skip to content

Create newtype for platform triple in bootstrap. #36696

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

Closed
wants to merge 1 commit into from

Conversation

frewsxcv
Copy link
Member

As I was reading through the bootstrap code, I noticed myself getting
lost at times trying to find out what the value of each &str is. I
also noticed there's quite a few platform-checking conditionals checking
the contents of these strings. In an attempt to clarify and cleanup
these cases, I created a 'newtype' Triple that represents a platform
triple and moved the platform-checking logic to methods on the new
struct.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

self.0.split('-').next().unwrap()
}

pub fn is_msvc(&self) -> bool {
Copy link
Member Author

Choose a reason for hiding this comment

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

Can some of these conditionals be combined? I was very conservative about not attempting to merge any.

/// * `i686-apple-darwin`
/// * `powerpc-unknown-linux-gnu`
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, RustcDecodable)]
pub struct Triple(pub String);
Copy link
Member Author

Choose a reason for hiding this comment

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

In the future, maybe this could be an enum that has all the platforms we support?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that would work due to the flexible / json target specifications.

@bors
Copy link
Collaborator

bors commented Sep 26, 2016

☔ The latest upstream changes (presumably #36442) made this pull request unmergeable. Please resolve the merge conflicts.

As I was reading through the bootstrap code, I noticed myself getting
lost at times trying to find out what the value of each `&str` is. I
also noticed there's quite a few platform-checking conditionals checking
the contents of these strings. In an attempt to clarify and cleanup
these cases, I created a 'newtype' `Triple` that represents a platform
triple and moved the platform-checking logic to methods on the new
struct.
@frewsxcv
Copy link
Member Author

Rebased.

@alexcrichton
Copy link
Member

Thanks for the PR! I've been somewhat hesitant to do this in the past though as I've not been super certain about what the benefit is and being wary of over-abstracting. This is already something that all build scripts have to deal with (e.g. they don't have a shared Triple type).

Could you elaborate though on some of the confusion you saw? I wonder if perhaps a pub struct Triple(str) could help where we just Deref to str to have a separate type

@frewsxcv
Copy link
Member Author

When I see a &str as a parameter, if it's not documented, I have to do some work to find out what the underlying content is and how it's formatted. If I see a Triple, I know exactly what sort of data I'm working with. To me, the newtype pattern acts like a compile-time comment; I find it especially helpful on loose types like &[u8] or &str.

Anyways, I don't feel strongly about this, mostly just throwing around patterns I personally find helpful.

@alexcrichton
Copy link
Member

Hm yeah it's true that &str isn't descriptive, but it's also what all build scripts are using anyway I think? (e.g. the "source of truth" is just a string). In that sense I hoped that names like target and host would convey "target triple", but perhaps the docs could be bolstered as well?

@frewsxcv
Copy link
Member Author

it's also what all build scripts are using anyway

Just to clarify, which build scripts are you referring to?

@alexcrichton
Copy link
Member

Oh just build scripts in general for Cargo crates, which the build system is somewhat intended to follow after

@frewsxcv
Copy link
Member Author

I'm not seeing anything about a target triple in the build script documentation, so I'm still not entirely sure what you're referring to here. Regardless, it doesn't sound like there's much enthusiasm around this change, so I'm leaning towards closing this. I'm curious to know your thoughts about the 'newtype' pattern since to me, this seemed like a good use of it.

@alexcrichton
Copy link
Member

Oh sorry I just meant env vars like $TARGET and $HOST.

@alexcrichton
Copy link
Member

I'm personally not a huge fan of newtypes as extra types in the limit just ends up forcing everyone to juggle a huge number of types in their heads, but when applied well they can certainly work nicely!

@frewsxcv
Copy link
Member Author

Sounds good, thanks for the input!

@frewsxcv frewsxcv closed this Sep 27, 2016
@frewsxcv frewsxcv deleted the bootstrap-target branch September 27, 2016 20:22
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.

5 participants