From 2edc341b29e1c0e9c216beda237b1971309a99d1 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 8 May 2019 11:05:38 +0200 Subject: [PATCH] src/tools.rs: Add AsAyn Trait --- src/api_schema/router.rs | 2 +- src/tools.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/api_schema/router.rs b/src/api_schema/router.rs index a2605e19..c6b77953 100644 --- a/src/api_schema/router.rs +++ b/src/api_schema/router.rs @@ -15,7 +15,7 @@ use super::api_handler::*; pub type BoxFut = Box, Error = failure::Error> + Send>; /// Abstract Interface for API methods to interact with the environment -pub trait RpcEnvironment { +pub trait RpcEnvironment: std::any::Any + crate::tools::AsAny + Send { /// Use this to pass additional result data. It is up to the environment /// how the data is used. diff --git a/src/tools.rs b/src/tools.rs index 54a7703d..c887b3cf 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -13,6 +13,7 @@ use std::path::Path; use std::io::Read; use std::io::ErrorKind; use std::time::Duration; +use std::any::Any; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; @@ -711,3 +712,14 @@ pub fn pipe() -> Result<(Fd, Fd), Error> { let (pin, pout) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?; Ok((Fd(pin), Fd(pout))) } + +/// An easy way to convert types to Any +/// +/// Mostly useful to downcast trait objects (see RpcEnvironment). +pub trait AsAny { + fn as_any(&self) -> &Any; +} + +impl AsAny for T { + fn as_any(&self) -> &Any { self } +}