1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Types for representing implementations of built-in traits.
//!
//! Note that when implementing a built-in trait, _you must ensure_ that you implement _all_ the
//! methods. The high-level API does not guard against leaving some methods unimplemented, like the
//! language interpreter does.

use crate::ffvariants::{FallibleSelf, ImmutableSelf, InfallibleSelf, MutableSelf};

/// Enumeration of all available built-in traits.
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub enum BuiltinTrait {
    None,
    Iterator,
}

mod private {
    pub trait Sealed {}
}

/// A built-in trait method that can be implemented with functions whose signature fits `S`.
pub trait BuiltinTraitFunction<S>: private::Sealed {
    #[doc(hidden)]
    const NAME: &'static str;

    #[doc(hidden)]
    fn owning_trait(&self) -> BuiltinTrait;
}

macro_rules! implement_builtin_trait_function {
    ($T:ty, $name:expr, ($($Args:ty),*), $owning_trait:expr) => {
        impl super::private::Sealed for $T {}

        macro_rules! implement_for {
            (<$S:tt> $ST:ty) => {
                impl<$S> BuiltinTraitFunction<$ST> for $T {
                    const NAME: &'static str = $name;

                    fn owning_trait(&self) -> BuiltinTrait {
                        $owning_trait
                    }
                }
            };
        }

        implement_for!(<S> FallibleSelf<ImmutableSelf<S>, (&S, $($Args),*)>);
        implement_for!(<S> InfallibleSelf<ImmutableSelf<S>, (&S, $($Args),*)>);
        implement_for!(<S> InfallibleSelf<MutableSelf<S>, (&mut S, $($Args),*)>);
        implement_for!(<S> FallibleSelf<MutableSelf<S>, (&mut S, $($Args),*)>);
    };
}

/// Methods from the `Iterator` trait.
pub mod iterator {
    use super::*;

    /// `Iterator.has_next`, can be implemented with `fn (&mut self) -> T`.
    #[derive(Debug)]
    pub struct HasNext;
    implement_builtin_trait_function!(HasNext, "has_next", (), BuiltinTrait::Iterator);

    /// `Iterator.next`, can be implemented with `fn (&mut self) -> T`.
    #[derive(Debug)]
    pub struct Next;
    implement_builtin_trait_function!(Next, "next", (), BuiltinTrait::Iterator);
}