-
-
Notifications
You must be signed in to change notification settings - Fork 305
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
Use OsString
everywhere
#1193
Use OsString
everywhere
#1193
Conversation
This is of course a massive breaking change, but I'm opening this so we can figure out if we want to make this change everywhere or to what degree it should be done. |
let friendly_name = match Self::os_version().unwrap_or_default().as_bytes() { | ||
f_n if f_n.starts_with(b"14.0") => "Sonoma", | ||
f_n if f_n.starts_with(b"10.16") | ||
| f_n.starts_with(b"11.0") | ||
| f_n.starts_with(b"11.1") | ||
| f_n.starts_with(b"11.2") => | ||
{ | ||
"Big Sur" | ||
} | ||
f_n if f_n.starts_with("10.15") => "Catalina", | ||
f_n if f_n.starts_with("10.14") => "Mojave", | ||
f_n if f_n.starts_with("10.13") => "High Sierra", | ||
f_n if f_n.starts_with("10.12") => "Sierra", | ||
f_n if f_n.starts_with("10.11") => "El Capitan", | ||
f_n if f_n.starts_with("10.10") => "Yosemite", | ||
f_n if f_n.starts_with("10.9") => "Mavericks", | ||
f_n if f_n.starts_with("10.8") => "Mountain Lion", | ||
f_n if f_n.starts_with("10.7") => "Lion", | ||
f_n if f_n.starts_with("10.6") => "Snow Leopard", | ||
f_n if f_n.starts_with("10.5") => "Leopard", | ||
f_n if f_n.starts_with("10.4") => "Tiger", | ||
f_n if f_n.starts_with("10.3") => "Panther", | ||
f_n if f_n.starts_with("10.2") => "Jaguar", | ||
f_n if f_n.starts_with("10.1") => "Puma", | ||
f_n if f_n.starts_with("10.0") => "Cheetah", | ||
f_n if f_n.starts_with(b"10.15") => "Catalina", | ||
f_n if f_n.starts_with(b"10.14") => "Mojave", | ||
f_n if f_n.starts_with(b"10.13") => "High Sierra", | ||
f_n if f_n.starts_with(b"10.12") => "Sierra", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remove older versions, because they are unsupported by Rust now. Also I renamed it to macOS
in the string, as that's the official way to write the name now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave them as other rust backends still support them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this PR as a whole (I read that you want it to be split up, so I'll take that into account) would require 1.74, which does not support any older versions of macOS.
s.rsplit(|&b| b == b':') | ||
.next() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rsplit.next should be faster considering we want the last one.
v.push(*i.offset(2)); | ||
v.push(*i.offset(3)); | ||
fn add_u32(v: &mut Vec<u8>, i: u32) { | ||
v.extend(i.to_ne_bytes()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be safe by simply using to_ne_bytes()
.
@@ -105,7 +104,7 @@ impl CpusWrapper { | |||
let mut parts = line.split(|x| *x == b' ').filter(|s| !s.is_empty()); | |||
if first { | |||
self.global_cpu.inner.name = | |||
to_str!(parts.next().unwrap_or(&[])).to_owned(); | |||
OsStr::from_bytes(parts.next().unwrap_or(&[])).to_owned(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous to_str!
also felt a little dubious here considering it just always assumed valid UTF-8, even though that's afaict not a guarantee that Linux gives here.
This makes it so `OsString` / `OsStr` are used everywhere instead of `String` / `str`, because the operating systems make no guarantee that the values provided are valid UTF-8. The `OsString` / `OsStr` types are specifically provided by the standard library for these situations. This fixes situations where `sysinfo` either had to lossily lose some of the information, had some subtle undefined behavior, and partially rejected processes and other resources entirely, making them impossible to be accessed by the user of this crate.
Please only update the process name information to |
@@ -1,10 +1,10 @@ | |||
task: | |||
name: rust 1.69 on freebsd 13 | |||
name: rust 1.74 on freebsd 13 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OsString
is really only usable since 1.74, so a MSRV bump is necessary. This PR isn't urgent, so feel free to delay this PR to whenever such a MSRV bump would be warranted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating the MSRV is fine since it'll be part of a "major" release in any case since it's a breaking change.
@GuillaumeGomez I'm going to split out the other changes, but are you rather tending towards not wanting them at all or is it just too large of a PR? Cause I do think that almost all of these should probably be I do at least somewhat agree that Maybe similar to how |
Too large for sure. I think overall we'll tend to replace all |
Alright, I'll close this and do individual PRs in a few hours. |
This makes it so
OsString
/OsStr
are used everywhere instead ofString
/str
, because the operating systems make no guarantee that the values provided are valid UTF-8. TheOsString
/OsStr
types are specifically provided by the standard library for these situations. This fixes situations wheresysinfo
either had to lossily lose some of the information, had some subtle undefined behavior, and partially rejected processes and other resources entirely, making them impossible to be accessed by the user of this crate.Resolves #1190