use core::fmt;
use core::marker::PhantomData;
use super::lang::Lang;
mod sealed {
pub trait Sealed {}
}
impl sealed::Sealed for FeaturePresent {}
impl sealed::Sealed for FeatureAbsent {}
pub trait FeaturePresence: sealed::Sealed + 'static {
const PRESENT: bool;
type Store<T: Clone + fmt::Debug + Send + Sync>: Clone + fmt::Debug + Send + Sync;
fn store_with<T: Clone + fmt::Debug + Send + Sync>(
make: impl FnOnce() -> T,
) -> Self::Store<T>;
fn store_get<T: Clone + fmt::Debug + Send + Sync>(store: &Self::Store<T>) -> Option<&T>;
fn store_get_mut<T: Clone + fmt::Debug + Send + Sync>(
store: &mut Self::Store<T>,
) -> Option<&mut T>;
fn store_into_inner<T: Clone + fmt::Debug + Send + Sync>(store: Self::Store<T>)
-> Option<T>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FeaturePresent;
impl FeaturePresence for FeaturePresent {
const PRESENT: bool = true;
type Store<T: Clone + fmt::Debug + Send + Sync> = T;
fn store_with<T: Clone + fmt::Debug + Send + Sync>(
make: impl FnOnce() -> T,
) -> Self::Store<T> {
make()
}
fn store_get<T: Clone + fmt::Debug + Send + Sync>(store: &Self::Store<T>) -> Option<&T> {
Some(store)
}
fn store_get_mut<T: Clone + fmt::Debug + Send + Sync>(
store: &mut Self::Store<T>,
) -> Option<&mut T> {
Some(store)
}
fn store_into_inner<T: Clone + fmt::Debug + Send + Sync>(
store: Self::Store<T>,
) -> Option<T> {
Some(store)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FeatureAbsent;
impl FeaturePresence for FeatureAbsent {
const PRESENT: bool = false;
type Store<T: Clone + fmt::Debug + Send + Sync> = PhantomData<T>;
fn store_with<T: Clone + fmt::Debug + Send + Sync>(
_make: impl FnOnce() -> T,
) -> Self::Store<T> {
PhantomData
}
fn store_get<T: Clone + fmt::Debug + Send + Sync>(_store: &Self::Store<T>) -> Option<&T> {
None
}
fn store_get_mut<T: Clone + fmt::Debug + Send + Sync>(
_store: &mut Self::Store<T>,
) -> Option<&mut T> {
None
}
fn store_into_inner<T: Clone + fmt::Debug + Send + Sync>(
_store: Self::Store<T>,
) -> Option<T> {
None
}
}
pub trait LangFeatures: 'static {
type Whitespace: FeaturePresence;
type Paragraphs: FeaturePresence;
type Groups: FeaturePresence;
type Commands: FeaturePresence;
type Comments: FeaturePresence;
type Specials: FeaturePresence;
type ForbiddenChars: FeaturePresence;
type Scopes: FeaturePresence;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AllLangFeatures;
impl LangFeatures for AllLangFeatures {
type Whitespace = FeaturePresent;
type Paragraphs = FeaturePresent;
type Groups = FeaturePresent;
type Commands = FeaturePresent;
type Comments = FeaturePresent;
type Specials = FeaturePresent;
type ForbiddenChars = FeaturePresent;
type Scopes = FeaturePresent;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NoLangFeatures;
impl LangFeatures for NoLangFeatures {
type Whitespace = FeatureAbsent;
type Paragraphs = FeatureAbsent;
type Groups = FeatureAbsent;
type Commands = FeatureAbsent;
type Comments = FeatureAbsent;
type Specials = FeatureAbsent;
type ForbiddenChars = FeatureAbsent;
type Scopes = FeatureAbsent;
}
pub trait LangHasWhitespace:
Lang<Features: LangFeatures<Whitespace = FeaturePresent>>
{
}
impl<L: Lang> LangHasWhitespace for L where
L::Features: LangFeatures<Whitespace = FeaturePresent>
{
}
pub trait LangHasParagraphs:
LangHasWhitespace + Lang<Features: LangFeatures<Paragraphs = FeaturePresent>>
{
}
impl<L: Lang> LangHasParagraphs for L where
L::Features: LangFeatures<Whitespace = FeaturePresent, Paragraphs = FeaturePresent>
{
}
pub trait LangHasGroups: Lang<Features: LangFeatures<Groups = FeaturePresent>> {}
impl<L: Lang> LangHasGroups for L where L::Features: LangFeatures<Groups = FeaturePresent>
{
}
pub trait LangHasCommands: Lang<Features: LangFeatures<Commands = FeaturePresent>> {}
impl<L: Lang> LangHasCommands for L where
L::Features: LangFeatures<Commands = FeaturePresent>
{
}
pub trait LangHasComments: Lang<Features: LangFeatures<Comments = FeaturePresent>> {}
impl<L: Lang> LangHasComments for L where
L::Features: LangFeatures<Comments = FeaturePresent>
{
}
pub trait LangHasSpecials: Lang<Features: LangFeatures<Specials = FeaturePresent>> {}
impl<L: Lang> LangHasSpecials for L where
L::Features: LangFeatures<Specials = FeaturePresent>
{
}
pub trait LangHasForbiddenChars:
Lang<Features: LangFeatures<ForbiddenChars = FeaturePresent>>
{
}
impl<L: Lang> LangHasForbiddenChars for L where
L::Features: LangFeatures<ForbiddenChars = FeaturePresent>
{
}
pub trait LangHasScopes: Lang<Features: LangFeatures<Scopes = FeaturePresent>> {}
impl<L: Lang> LangHasScopes for L where L::Features: LangFeatures<Scopes = FeaturePresent>
{
}
#[cfg(test)]
mod compile_checks {
use super::*;
use crate::scopes::{ScopeOp, SpecsProvider};
use crate::state::{
CommandOverrides, CommentOverrides, ForbiddenCharsOverrides, GroupOverrides,
ParagraphOverrides, SpecialsOverrides, TrivialLang, WhitespaceOverrides,
};
use crate::token::{
CommandRules, CommentRules, ForbiddenCharsRules, GroupRules, ParagraphRules,
PrefixTable, SpecialsRules, TriggerChars, WhitespaceRules,
};
use alloc::sync::Arc;
use alloc::vec::Vec;
#[derive(Debug, Clone, Copy)]
struct FeatLang;
impl TrivialLang for FeatLang {}
fn assert_store_payload<T: Clone + fmt::Debug + Send + Sync>() {}
const _: &[fn()] = &[
assert_store_payload::<WhitespaceRules>,
assert_store_payload::<ParagraphRules>,
assert_store_payload::<GroupRules<FeatLang>>,
assert_store_payload::<CommandRules>,
assert_store_payload::<CommentRules>,
assert_store_payload::<SpecialsRules>,
assert_store_payload::<ForbiddenCharsRules>,
assert_store_payload::<WhitespaceOverrides>,
assert_store_payload::<ParagraphOverrides>,
assert_store_payload::<GroupOverrides<FeatLang>>,
assert_store_payload::<CommandOverrides>,
assert_store_payload::<CommentOverrides>,
assert_store_payload::<SpecialsOverrides>,
assert_store_payload::<ForbiddenCharsOverrides>,
assert_store_payload::<Vec<ScopeOp<FeatLang>>>,
assert_store_payload::<Vec<Arc<dyn SpecsProvider<FeatLang>>>>,
assert_store_payload::<Arc<PrefixTable<FeatLang>>>,
assert_store_payload::<TriggerChars>,
];
fn projection_round_trip<P: FeaturePresence>() -> Option<CommandRules> {
let mut store = P::store_with(CommandRules::empty);
let _: Option<&CommandRules> = P::store_get(&store);
if let Some(rules) = P::store_get_mut(&mut store) {
rules.enabled = true;
}
P::store_into_inner(store)
}
const _: fn() -> Option<CommandRules> = projection_round_trip::<FeaturePresent>;
const _: fn() -> Option<CommandRules> = projection_round_trip::<FeatureAbsent>;
const _: () = assert!(FeaturePresent::PRESENT);
const _: () = assert!(!FeatureAbsent::PRESENT);
const fn every_member_present<F: LangFeatures>() -> bool {
<F::Whitespace as FeaturePresence>::PRESENT
&& <F::Paragraphs as FeaturePresence>::PRESENT
&& <F::Groups as FeaturePresence>::PRESENT
&& <F::Commands as FeaturePresence>::PRESENT
&& <F::Comments as FeaturePresence>::PRESENT
&& <F::Specials as FeaturePresence>::PRESENT
&& <F::ForbiddenChars as FeaturePresence>::PRESENT
&& <F::Scopes as FeaturePresence>::PRESENT
}
const fn no_member_present<F: LangFeatures>() -> bool {
!<F::Whitespace as FeaturePresence>::PRESENT
&& !<F::Paragraphs as FeaturePresence>::PRESENT
&& !<F::Groups as FeaturePresence>::PRESENT
&& !<F::Commands as FeaturePresence>::PRESENT
&& !<F::Comments as FeaturePresence>::PRESENT
&& !<F::Specials as FeaturePresence>::PRESENT
&& !<F::ForbiddenChars as FeaturePresence>::PRESENT
&& !<F::Scopes as FeaturePresence>::PRESENT
}
const _: () = assert!(every_member_present::<AllLangFeatures>());
const _: () = assert!(no_member_present::<NoLangFeatures>());
const _: () = assert!(
core::mem::size_of::<<FeatureAbsent as FeaturePresence>::Store<CommandRules>>() == 0
);
const _: () = assert!(
core::mem::size_of::<<FeaturePresent as FeaturePresence>::Store<u64>>()
== core::mem::size_of::<u64>()
);
struct GroupStoreProbe<L: Lang> {
payload: <<L::Features as LangFeatures>::Groups as FeaturePresence>::Store<Vec<u32>>,
}
fn plain_literal_under_the_bound<L: LangHasGroups>() -> GroupStoreProbe<L> {
GroupStoreProbe { payload: alloc::vec![1, 2, 3] }
}
fn plain_read_under_the_bound<L: LangHasGroups>(probe: &GroupStoreProbe<L>) -> usize {
probe.payload.len()
}
fn const_guard_unbounded<L: Lang>() -> bool {
<L::Features as LangFeatures>::Groups::PRESENT
}
fn requires_every_feature<L>()
where
L: LangHasWhitespace
+ LangHasParagraphs
+ LangHasGroups
+ LangHasCommands
+ LangHasComments
+ LangHasSpecials
+ LangHasForbiddenChars
+ LangHasScopes,
{
}
const _: fn() -> GroupStoreProbe<FeatLang> = plain_literal_under_the_bound::<FeatLang>;
const _: fn(&GroupStoreProbe<FeatLang>) -> usize = plain_read_under_the_bound::<FeatLang>;
const _: fn() -> bool = const_guard_unbounded::<FeatLang>;
const _: fn() = requires_every_feature::<FeatLang>;
}