Skip to content

Commit

Permalink
Remove unnecessary allocations (#68)
Browse files Browse the repository at this point in the history
* Remove unnecessary allocations

* Add illumos to CI

* Fix illumos build (and illumos bug)

* Fix CI for WebAssembly on Rust 1.40
  • Loading branch information
AldaronLau authored Aug 17, 2023
1 parent e4fa92e commit 188c446
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
- wasm32-wasi
- x86_64-apple-darwin
- x86_64-unknown-redox
- x86_64-unknown-illumos
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -186,6 +187,25 @@ jobs:
- run: cargo update -p js-sys --precise 0.3.55
- run: cargo update -p wasm-bindgen --precise 0.2.78
- run: cargo update -p log --precise 0.4.17
- run: cargo update -p quote --precise 1.0.30
- run: cargo update -p proc-macro2 --precise 1.0.63
- run: cargo build --all-features --target=${{ matrix.cc }}
- run: cargo build --no-default-features --target=${{ matrix.cc }}
- run: RUSTFLAGS="--cfg target_os=\"daku\"" cargo build --target=${{ matrix.cc }}
cross-compile-illumos:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
tc: [1.65.0, stable, beta, nightly]
cc:
- x86_64-unknown-illumos
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.tc }}
target: ${{ matrix.cc }}
override: true
- run: cargo build --all-features --target=${{ matrix.cc }}
24 changes: 10 additions & 14 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,9 @@ pub(crate) fn devicename_os() -> OsString {

#[cfg(not(any(target_os = "macos", target_os = "illumos")))]
pub(crate) fn devicename() -> String {
let mut distro = String::new();

if let Ok(program) = std::fs::read_to_string("/etc/machine-info") {
let program = program.into_bytes();

distro.push_str(&String::from_utf8_lossy(&program));
let distro = String::from_utf8_lossy(&program);

for i in distro.split('\n') {
let mut j = i.split('=');
Expand All @@ -330,24 +327,27 @@ pub(crate) fn devicename_os() -> OsString {
let out = os_from_cfstring(unsafe {
SCDynamicStoreCopyComputerName(null_mut(), null_mut())
});

let computer = if out.as_bytes().is_empty() {
Err(hostname_os())
} else {
Ok(out)
};

fancy_fallback_os(computer)
}

#[cfg(target_os = "illumos")]
pub(crate) fn devicename() -> String {
let mut nodename = String::new();

if let Ok(program) = std::fs::read_to_string("/etc/nodename") {
let program = program.into_bytes();
nodename.push_str(&String::from_utf8_lossy(&program));
nodename.pop(); // Remove the trailing newline
let mut nodename = String::from_utf8_lossy(&program).to_string();

// Remove the trailing newline
nodename.pop();

return nodename;
}

fancy_fallback(Err(hostname()))
}

Expand Down Expand Up @@ -444,14 +444,10 @@ pub(crate) fn distro_os() -> Option<OsString> {

#[cfg(not(target_os = "macos"))]
pub(crate) fn distro() -> Option<String> {
let mut distro = String::new();

let program = std::fs::read_to_string("/etc/os-release")
.ok()?
.into_bytes();

distro.push_str(&String::from_utf8_lossy(&program));

let distro = String::from_utf8_lossy(&program);
let mut fallback = None;

for i in distro.split('\n') {
Expand Down

0 comments on commit 188c446

Please sign in to comment.