From adf6b9f7be9a59be129869f4c63d33b10c80e23f Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sat, 16 Jan 2021 10:12:13 -0300 Subject: Workaround lack of GATs --- src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index f41b4c6..08609ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,21 +5,49 @@ mod vm; pub use pattern::Pattern; +// TODO replace with GATs +/// The key returned from pairs and get. +pub enum Key<'b, T, U> { + /// Reference. + Ref(&'b T), + /// Borrowed string. + Str(&'b str), + /// Owned types. + Own(U), +} + +impl<'b, T, U: Copy> Copy for Key<'b, T, U> { +} + +impl<'b, T, U: Clone> Clone for Key<'b, T, U> { + fn clone(&self) -> Self { + match self { + Key::Ref(r) => Key::Ref(r), + Key::Str(r) => Key::Str(r), + Key::Own(v) => Key::Own(v.clone()), + } + } +} + +pub type KVPair<'b, T> = (Key<'b, ::Value, ::OwnKey>, &'b ::Value); + // TODO investigate if this should be PatternTypes: Default /// Defines the types and operations used for matching. pub trait PatternTypes { + // TODO investigate Value: ?Sized /// The value type. type Value; - /// The owned key type. May be uninhabited. // TODO replace with GATs. - type Key; + // TODO potentially relax with Clone? + /// The owned key type. May be uninhabited. + type OwnKey: Copy + 'static; /// Returns an iterator over key-value pairs contained within an item, or /// None if this operation is unsupported for the given value. fn pairs<'b>( item: &'b Self::Value - ) -> Option + 'b>> { + ) -> Option> + 'b>> { // TODO remove these default impls that only exist for testing purposes let x = None; Some(Box::new(x.into_iter())) @@ -29,16 +57,20 @@ pub trait PatternTypes { /// this operation is unsupported for the given value. fn get<'a, 'b>( item: &'b Self::Value, - key: &'a str - ) -> Option> { + key: Key<'a, Self::Value, Self::OwnKey> + ) -> Option>> { // TODO remove these default impls that only exist for testing purposes Some(None) } - /// Returns whether two values are the same/equivalent. This must provide + // TODO replace with GATs + newtypes + /// Returns whether two keys/values are the same/equivalent. This must provide /// the same guarantees as PartialEq. In fact, this is a replacement for /// PartialEq for cases where it's not possible to just use PartialEq. - fn matches(left: &Self::Value, right: &Self::Value) -> bool; + fn matches( + left: Key<'_, Self::Value, Self::OwnKey>, + right: Key<'_, Self::Value, Self::OwnKey> + ) -> bool; } // TODO -- cgit 1.4.1