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

Add Signal::as_str() to get representation as static string #1138

Merged
merged 1 commit into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - ReleaseDate
### Added
- Added `Signal::as_str()`: returns signal name as `&'static str`
(#[1138](https://github.com/nix-rust/nix/pull/1138))

- Added `posix_fallocate`.
([#1105](https://github.com/nix-rust/nix/pull/1105))
Expand Down
17 changes: 14 additions & 3 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ impl FromStr for Signal {
}
}

impl AsRef<str> for Signal {
fn as_ref(&self) -> &str {
match *self {
impl Signal {
/// Returns name of signal.
///
/// This function is equivalent to `<Signal as AsRef<str>>::as_ref()`,
/// with difference that returned string is `'static`
/// and not bound to `self`'s lifetime.
pub fn as_str(self) -> &'static str {
match self {
Signal::SIGHUP => "SIGHUP",
Signal::SIGINT => "SIGINT",
Signal::SIGQUIT => "SIGQUIT",
Expand Down Expand Up @@ -157,6 +162,12 @@ impl AsRef<str> for Signal {
}
}

impl AsRef<str> for Signal {
fn as_ref(&self) -> &str {
self.as_str()
}
}

impl fmt::Display for Signal {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.as_ref())
Expand Down