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

Support arbitrary baud rates on BSDs #843

Merged
merged 3 commits into from
Jan 26, 2018
Merged

Conversation

Susurrus
Copy link
Contributor

@Susurrus Susurrus commented Jan 24, 2018

Closes #842

@Susurrus Susurrus force-pushed the arb_baud branch 5 times, most recently from 133cfa8 to 08d1228 Compare January 25, 2018 05:08
@Susurrus
Copy link
Contributor Author

It looks like things are still not correct for mac platforms; it needs to be decided if u32 is a fine output type or if it should really be speed_t, which is 32 on 32-bit platforms and 64 on 64-bit platforms. Considering the u32 covers up to 4 billion baud in this scenario, I'm going to be it's fine using u32 across all platforms.

@Susurrus Susurrus force-pushed the arb_baud branch 2 times, most recently from ec16106 to 06d0c21 Compare January 25, 2018 05:42
@asomers
Copy link
Member

asomers commented Jan 25, 2018

If you implement TryFrom<u32> for BaudRate, then it will be easier to write portable code. The following would work on all platforms:

cfsetspeed(&mut t, BaudRate::B9600);
let b : BaudRate = cfgetispeed(&t).try_into().unwrap();

@Susurrus
Copy link
Contributor Author

TryFrom is nightly-only. I'll add a FIXME to change the API in the future.

@Susurrus
Copy link
Contributor Author

I'll take that as an implicit review @asomers, but cancel this merge if I shouldn't have.

bors r+

bors bot added a commit that referenced this pull request Jan 26, 2018
843: Support arbitrary baud rates on BSDs r=Susurrus a=Susurrus

Closes #842
@asomers
Copy link
Member

asomers commented Jan 26, 2018

Not yet! That's was just what I could come up with while still at work. I'm going to do the full review now.

bors cancel

@Susurrus
Copy link
Contributor Author

bors r-

@bors
Copy link
Contributor

bors bot commented Jan 26, 2018

Canceled

@@ -354,6 +475,13 @@ impl From<libc::speed_t> for BaudRate {
}
}

// TODO: Include `TryFrom<u32> for BaudRate` once that API stabilizes
impl From<BaudRate> for u32 {
Copy link
Member

Choose a reason for hiding this comment

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

On Linux this function will return something very different than what it returns on the BSDs. I don't think that's what we want. We should probably only implement this trait on the BSDs. On Linux, BaudRate is opaque.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I originally meant to only expose this on BSDs.

/// Get output baud rate (see
/// [cfgetospeed(3p)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/cfgetospeed.html)).
///
/// `cfgetospeed()` extracts the output baud rate from the given Termios structure.
Copy link
Member

Choose a reason for hiding this comment

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

Termios should be in backticks here and elsewhere.

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 don't think it should, Termios is not a struct, termios is, Termios is the name of the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

NVM, I agree with what you're saying.

}

/// Set both the input and output baud rates (see
/// [termios(3)](http://man7.org/linux/man-pages/man3/termios.3.html)).
Copy link
Member

Choose a reason for hiding this comment

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

It's silly to use a Linux man page link in the documentation for non-Linux operating systems. How about https://www.freebsd.org/cgi/man.cgi?query=cfsetspeed instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, corrected.

let inner_termios = unsafe { termios.get_libc_termios_mut() };
let res = unsafe { libc::cfsetospeed(inner_termios, baud as libc::speed_t) };
termios.update_wrapper();
Errno::result(res).map(drop)
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I find .map(|_| ()) to be more intuitive than .map(drop), though it is a little longer.

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 agree.

Bryant Mairs added 3 commits January 25, 2018 20:37
.map(drop) makes it seem like there is a need to drop the type at
that point when really all you're doing is trying to erase the
type information and return an empty tuple. So do that explicitly
instead of throwing drop in there.
@Susurrus
Copy link
Contributor Author

@asomers Take a look-see now, might be more to your liking.

@asomers
Copy link
Member

asomers commented Jan 26, 2018

bors r+

bors bot added a commit that referenced this pull request Jan 26, 2018
843: Support arbitrary baud rates on BSDs r=asomers a=Susurrus

Closes #842
@bors
Copy link
Contributor

bors bot commented Jan 26, 2018

@bors bors bot merged commit 5f7a43a into nix-rust:master Jan 26, 2018
@Susurrus Susurrus deleted the arb_baud branch February 27, 2018 04:55
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.

None yet

2 participants