From e7aaec8e3a686364a00045bd0c53661c37871050 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Sat, 25 Jul 2020 09:14:29 +0200 Subject: [PATCH] Seal Variant trait to prevent downstream implementations --- src/any.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/any.rs b/src/any.rs index 7f637187..202afa24 100644 --- a/src/any.rs +++ b/src/any.rs @@ -30,12 +30,24 @@ use crate::stdlib::time::Instant; #[cfg(target_arch = "wasm32")] use instant::Instant; +mod private { + use crate::fn_native::SendSync; + use crate::stdlib::any::Any; + + /// A sealed trait that prevents other crates from implementing [Variant]. + /// + /// [Variant]: super::Variant + pub trait Sealed {} + + impl Sealed for T {} +} + /// Trait to represent any type. /// /// Currently, `Variant` is not `Send` nor `Sync`, so it can practically be any type. /// Turn on the `sync` feature to restrict it to only types that implement `Send + Sync`. #[cfg(not(feature = "sync"))] -pub trait Variant: Any { +pub trait Variant: Any + private::Sealed { /// Convert this `Variant` trait object to `&dyn Any`. fn as_any(&self) -> &dyn Any; @@ -53,10 +65,6 @@ pub trait Variant: Any { /// Clone into `Dynamic`. fn clone_into_dynamic(&self) -> Dynamic; - - /// This trait may only be implemented by `rhai`. - #[doc(hidden)] - fn _closed(&self) -> _Private; } /// Trait to represent any type. @@ -64,7 +72,7 @@ pub trait Variant: Any { /// `From<_>` is implemented for `i64` (`i32` if `only_i32`), `f64` (if not `no_float`), /// `bool`, `String`, `char`, `Vec` (into `Array`) and `HashMap` (into `Map`). #[cfg(feature = "sync")] -pub trait Variant: Any + Send + Sync { +pub trait Variant: Any + Send + Sync + private::Sealed { /// Convert this `Variant` trait object to `&dyn Any`. fn as_any(&self) -> &dyn Any; @@ -82,10 +90,6 @@ pub trait Variant: Any + Send + Sync { /// Clone into `Dynamic`. fn clone_into_dynamic(&self) -> Dynamic; - - /// This trait may only be implemented by `rhai`. - #[doc(hidden)] - fn _closed(&self) -> _Private; } impl Variant for T { @@ -107,9 +111,6 @@ impl Variant for T { fn clone_into_dynamic(&self) -> Dynamic { Dynamic::from(self.clone()) } - fn _closed(&self) -> _Private { - _Private - } } impl dyn Variant {