Pre: introduce FromRc to convert between refcounted types.

This commit is contained in:
Richard Newman 2018-04-25 12:21:29 -07:00
parent 4a42d2ca2c
commit 9292867dc9

View file

@ -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<T> {
fn from_rc(val: Rc<T>) -> Self;
fn from_arc(val: Arc<T>) -> Self;
}
impl<T> FromRc<T> for Rc<T> where T: Sized + Clone {
fn from_rc(val: Rc<T>) -> Self {
val.clone()
}
fn from_arc(val: Arc<T>) -> Self {
match ::std::sync::Arc::<T>::try_unwrap(val) {
Ok(v) => Self::new(v),
Err(r) => Self::new((*r.as_ref()).clone()),
}
}
}
impl<T> FromRc<T> for Arc<T> where T: Sized + Clone {
fn from_rc(val: Rc<T>) -> Self {
match ::std::rc::Rc::<T>::try_unwrap(val) {
Ok(v) => Self::new(v),
Err(r) => Self::new((*r.as_ref()).clone()),
}
}
fn from_arc(val: Arc<T>) -> 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