Skip to content

Commit 34f9ea2

Browse files
committed
Add impl Default for ProcResult and platform-specific structs.
1 parent c05bb0b commit 34f9ea2

File tree

6 files changed

+54
-16
lines changed

6 files changed

+54
-16
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.2.2] - 2025-06-06
9+
10+
### Added
11+
12+
- Added `Default` to `ProcResult`, `unix::WaitStatus`, and `windows::ExitCode`.
13+
14+
### Changed
15+
16+
- `impl From<std::process::ExitCode> for ProcResult` is now conditional on
17+
`unix` or `windows`.
18+
819
## [0.2.1] - 2025-06-01
920

1021
### Changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "proc-result"
33
description = "A tiny cross-platform library containing exit status and code types"
44
repository = "http://crates.lurey.io/proc-result"
55
license = "MIT"
6-
version = "0.2.1"
6+
version = "0.2.2"
77
edition = "2024"
88
keywords = ["cli", "exit-codes", "sysexit"]
99
categories = [

src/lib.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ impl ProcResult {
6464
}
6565
}
6666

67+
#[cfg(all(feature = "std", unix))]
68+
impl Default for ProcResult {
69+
fn default() -> Self {
70+
Self::Unix(unix::WaitStatus::default())
71+
}
72+
}
73+
74+
#[cfg(all(feature = "std", windows))]
75+
impl Default for ProcResult {
76+
fn default() -> Self {
77+
Self::Windows(windows::ExitCode::default())
78+
}
79+
}
80+
6781
#[cfg(feature = "std")]
6882
impl Display for ProcResult {
6983
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
@@ -76,20 +90,33 @@ impl Display for ProcResult {
7690

7791
impl core::error::Error for ProcResult {}
7892

79-
#[cfg(feature = "std")]
93+
#[cfg(all(feature = "std", unix))]
8094
impl From<std::process::ExitStatus> for ProcResult {
8195
#[allow(unreachable_code)]
8296
fn from(status: std::process::ExitStatus) -> Self {
83-
#[cfg(unix)]
84-
{
85-
let err: unix::WaitStatus = status.into();
86-
return Self::Unix(err);
87-
}
88-
#[cfg(windows)]
89-
{
90-
let err: windows::ExitCode = status.into();
91-
return Self::Windows(err);
92-
}
93-
panic!("Cannot convert exit status to error on this platform");
97+
Self::Unix(status.into())
98+
}
99+
}
100+
101+
#[cfg(all(feature = "std", windows))]
102+
impl From<std::process::ExitStatus> for ProcResult {
103+
#[allow(unreachable_code)]
104+
fn from(status: std::process::ExitStatus) -> Self {
105+
Self::Windows(status.into())
106+
}
107+
}
108+
109+
#[cfg(test)]
110+
mod tests {
111+
#[test]
112+
#[cfg(feature = "std")]
113+
fn test_default_proc_result() {
114+
use super::ProcResult;
115+
116+
let result = ProcResult::default();
117+
assert!(
118+
result.is_success(),
119+
"Default ProcResult should indicate success"
120+
);
94121
}
95122
}

src/unix/wait_status.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{ExitCode, Signal, WaitState};
55
/// On Unix-like systems, processes can terminate with a combination of exit codes and signals;
66
/// this struct encapsulates that information and can separate the exit code from the signal that
77
/// caused the termination, or whether the process was stopped or continued.
8-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
99
#[cfg_attr(
1010
feature = "serde",
1111
derive(serde::Serialize, serde::Deserialize),

src/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::raw::RawExitCode;
77
use core::fmt::Display;
88

99
/// A Windows-specific exit code.
10-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
1111
#[cfg_attr(
1212
feature = "serde",
1313
derive(serde::Serialize, serde::Deserialize),

0 commit comments

Comments
 (0)