diff --git a/core/src/types.rs b/core/src/types.rs index b8d0a2bf..fee9cda1 100644 --- a/core/src/types.rs +++ b/core/src/types.rs @@ -8,9 +8,15 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. -use std::fmt; -use std::rc::Rc; +use ::std::rc::{ + Rc, +}; +use ::std::sync::{ + Arc, +}; + +use std::fmt; use ::enum_set::EnumSet; use ::ordered_float::OrderedFloat; @@ -35,6 +41,37 @@ use ::edn::{ use values; +pub trait FromRc { + fn from_rc(val: Rc) -> Self; + fn from_arc(val: Arc) -> Self; +} + +impl FromRc for Rc where T: Sized + Clone { + fn from_rc(val: Rc) -> Self { + val.clone() + } + + fn from_arc(val: Arc) -> Self { + match ::std::sync::Arc::::try_unwrap(val) { + Ok(v) => Self::new(v), + Err(r) => Self::new((*r.as_ref()).clone()), + } + } +} + +impl FromRc for Arc where T: Sized + Clone { + fn from_rc(val: Rc) -> Self { + match ::std::rc::Rc::::try_unwrap(val) { + Ok(v) => Self::new(v), + Err(r) => Self::new((*r.as_ref()).clone()), + } + } + + fn from_arc(val: Arc) -> Self { + val.clone() + } +} + /// Represents one entid in the entid space. /// /// Per https://www.sqlite.org/datatype3.html (see also http://stackoverflow.com/a/8499544), SQLite