diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-29 14:47:49 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-29 14:47:49 +0000 |
commit | 7a5248c70a4ad0ef1ff1b385a7674b38403386df (patch) | |
tree | 327e4acd06ba2ead6ee3f1bf972430cb250abdf0 /src | |
parent | 2e52ce8153154650dcf1d4ae8e7738b4f24a34e3 (diff) | |
parent | bacb85ecd958bbf4ca013d900dd33562e7102f56 (diff) | |
download | nix-7a5248c70a4ad0ef1ff1b385a7674b38403386df.zip |
Merge #1138
1138: Add Signal::as_str() to get representation as static string r=asomers a=MikailBag
# Motivation
Currently string representation of signal can be obtained with AsRef<str> impl.
But it has downside: returned string's lifetime is bound to lifetime of signal, so &str must be converted to String. This allocation is avoidable, because as_ref() only returns static strings. To fix this problem, my PR adds Signal::as_str() method, which returns static string.
Co-authored-by: Mikail Bagishov <bagishov.mikail@yandex.ru>
Diffstat (limited to 'src')
-rw-r--r-- | src/sys/signal.rs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/sys/signal.rs b/src/sys/signal.rs index 1224888e..b746b3d4 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -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", @@ -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()) |