Skip to content

Repository files navigation

::ghost_lite

A lightweight implementation of ::ghost with macro_rules.

Usage

use ghost_lite::ghost;ghost!{/// `ghost` macro defines a custom `PhantomData`,/// which can be used as both a type and a value.pubstructMyPhantomData<T>
}fnmain(){let _:MyPhantomData<i32> = MyPhantomData;let _ = MyPhantomData::<&str>;}

Caveats

  1. derive should be in inner attributes (#![derive(...)]). Only the following traits are supported.

    ghost_lite::ghost! {
    #![derive(Clone,Copy,Default,Hash,PartialOrd,Ord,PartialEq,Eq,Debug)]pubstructMyPhantom<T>
    }/// `String` is not copy, but `MyPhantom` is always Copy, like `PhantomData`fntest() -> implCopy{MyPhantom::<String>}
    # fnmain(){}

    derive in outer attributes will be directly prepended to the generated enum, which works like normal derive macros.

    ghost_lite::ghost! {/// MyPhantom is `Clone` and `Copy` only if T is `Clone` and `Copy` #[derive(Clone,Copy)]pubstructMyPhantom<T>
    }/// `String` is not copy, so `MyPhantom` is not Copyfntest() -> implCopy{MyPhantom::<String>}
    # fnmain(){}
  2. The implementation relies on a mod name. To define multiple custom PhantomData types in the same module, you must provide custom mod name with #![mod_value_namespace = my_phantom_data].

    ghost_lite::ghost! {structMyPhantomData1<T>
    }
    ghost_lite::ghost! {structMyPhantomData2<T>
    }
    # fnmain(){}
    ghost_lite::ghost! {structMyPhantomData1<T>
    }
    ghost_lite::ghost! {
    #![mod_value_namespace = my_phantom_data_2]structMyPhantomData2<T>
    }
    # fnmain(){}
  3. Move type generic bounds to where clause if ghost! reports error.

    Parsing tokens is limited with macro_rules, so complex type bounds are not supported.

    For example:

    ghost_lite::ghost! {structMyPhantomData<T:Clone + PartialEq>
    }
    # fnmain(){}
    ghost_lite::ghost! {structMyPhantomData<T> whereT:Clone + PartialEq}
    # fnmain(){}
  4. Please don't add a trailing semicolon ;.

    ghost_lite::ghost! {structMyPhantomData<T>;}
    # fnmain(){}
    ghost_lite::ghost! {structMyPhantomData<T>
    }
    # fnmain(){}

About

Custom PhantomData types

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages