mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
re foreign trait constraints
This commit is contained in:
parent
ddf0c05d5f
commit
cbd28a5934
10 changed files with 30 additions and 24 deletions
|
|
@ -18,17 +18,23 @@ pub trait Give<Type> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'n, T: Take<'n, U>, U> Give<T> for U {
|
||||
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<T> {
|
||||
T::take(self, words)
|
||||
}
|
||||
}
|
||||
|
||||
/// [Give]s instances of [Self] given [TokenIter].
|
||||
pub trait Take<'n, State>: Sized + 'n {
|
||||
fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self>;
|
||||
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self>;
|
||||
/// Return custom error on [None].
|
||||
fn provide_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||
fn take_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||
state: &State, words: &mut TokenIter<'source>, error: F
|
||||
) -> Usually<Self> {
|
||||
if let Some(value) = Take::<State>::provide(state, words)? {
|
||||
if let Some(value) = Take::<State>::take(state, words)? {
|
||||
Ok(value)
|
||||
} else {
|
||||
Result::Err(format!("provide: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
|
||||
Result::Err(format!("take: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -38,21 +44,21 @@ pub trait Take<'n, State>: Sized + 'n {
|
|||
#[macro_export] macro_rules! from_dsl {
|
||||
(@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
|
||||
impl<'n, State, A: Take<'n, State>> Take<'n, State> for $T {
|
||||
fn provide <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
$expr
|
||||
}
|
||||
}
|
||||
};
|
||||
(@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
|
||||
impl<'n, State, A: Take<'n, State>, B: Take<'n, State>> Take<'n, State> for $T {
|
||||
fn provide <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
$expr
|
||||
}
|
||||
}
|
||||
};
|
||||
($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => {
|
||||
impl<'n> Take<'n, $S> for $T {
|
||||
fn provide <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
fn take <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
$expr
|
||||
}
|
||||
}
|
||||
|
|
@ -62,7 +68,7 @@ pub trait Take<'n, State>: Sized + 'n {
|
|||
// auto impl graveyard:
|
||||
|
||||
//impl<'n, State: Give<Type>, Type: 'n> Take<'n, State> for Type {
|
||||
//fn provide <'source> (state: &State, words: &mut TokenIter<'source>)
|
||||
//fn take <'source> (state: &State, words: &mut TokenIter<'source>)
|
||||
//-> Perhaps<Self>
|
||||
//{
|
||||
//state.take(words)
|
||||
|
|
@ -71,6 +77,6 @@ pub trait Take<'n, State>: Sized + 'n {
|
|||
|
||||
//impl<'n, Type: Take<'n, State>, State> Give<Type> for State {
|
||||
//fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
|
||||
//Type::provide(self, words)
|
||||
//Type::take(self, words)
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue