From 2d812cf3381a2d910b0a24265bee8b7d4ad059ea Mon Sep 17 00:00:00 2001 From: Fabian Freyer Date: Sat, 30 Mar 2019 21:57:31 -0400 Subject: [PATCH] feature: Implement TryFrom (#7) --- CHANGELOG.md | 1 + src/running.rs | 9 +++++++++ src/stopped.rs | 9 +++++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb68c3da2..77c6f3739 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added +* implementations for `TryFrom` to start / stop jails. * example showing how to query RCTL usage. * code coverage with codecov.io. Unfortunately, this doesn't yet take docstests into account, so coverage is actually a bit better in reality. diff --git a/src/running.rs b/src/running.rs index 999d2457c..131961753 100644 --- a/src/running.rs +++ b/src/running.rs @@ -6,6 +6,7 @@ use JailError; use StoppedJail; use std::collections::HashMap; +use std::convert::TryFrom; use std::io::{Error, ErrorKind}; use std::net; use std::path; @@ -545,6 +546,14 @@ impl RunningJail { } } +impl TryFrom for RunningJail { + type Error = JailError; + + fn try_from(stopped: StoppedJail) -> Result { + stopped.start() + } +} + /// An Iterator over running Jails /// /// See [RunningJail::all()](struct.RunningJail.html#method.all) for a usage diff --git a/src/stopped.rs b/src/stopped.rs index 060683041..73cbe9ebc 100644 --- a/src/stopped.rs +++ b/src/stopped.rs @@ -11,6 +11,7 @@ use RunningJail; #[cfg(feature = "serialize")] use serde::Serialize; +use std::convert::TryFrom; use std::fmt; /// Represent a stopped jail including all information required to start it @@ -52,6 +53,14 @@ impl Default for StoppedJail { } } +impl TryFrom for StoppedJail { + type Error = JailError; + + fn try_from(running: RunningJail) -> Result { + running.stop() + } +} + #[cfg(target_os = "freebsd")] impl StoppedJail { /// Create a new Jail instance given a path.