Skip to content

Commit

Permalink
Expose internal CustomToken trait only through __private module
Browse files Browse the repository at this point in the history
This trait is marked not public API and can only be implemented through
the public macros (custom_keyword and custom_punctuation).
  • Loading branch information
dtolnay committed Nov 3, 2023
1 parent f7b79f5 commit eb1737d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/custom_keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ macro_rules! custom_keyword {
macro_rules! impl_parse_for_custom_keyword {
($ident:ident) => {
// For peek.
impl $crate::token::CustomToken for $ident {
impl $crate::__private::CustomToken for $ident {
fn peek(cursor: $crate::buffer::Cursor) -> $crate::__private::bool {
if let $crate::__private::Some((ident, _rest)) = cursor.ident() {
ident == $crate::__private::stringify!($ident)
Expand Down
2 changes: 1 addition & 1 deletion src/custom_punctuation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ macro_rules! custom_punctuation {
#[macro_export]
macro_rules! impl_parse_for_custom_punctuation {
($ident:ident, $($tt:tt)+) => {
impl $crate::token::CustomToken for $ident {
impl $crate::__private::CustomToken for $ident {
fn peek(cursor: $crate::buffer::Cursor) -> $crate::__private::bool {
$crate::__private::peek_punct(cursor, $crate::stringify_punct!($($tt)+))
}
Expand Down
4 changes: 4 additions & 0 deletions src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ pub use crate::token::parsing::{peek_punct, punct as parse_punct};
#[doc(hidden)]
pub use crate::token::printing::punct as print_punct;

#[cfg(feature = "parsing")]
#[doc(hidden)]
pub use crate::token::private::CustomToken;

#[cfg(feature = "proc-macro")]
#[doc(hidden)]
pub type TokenStream = proc_macro::TokenStream;
Expand Down
22 changes: 13 additions & 9 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
//! [Printing]: https://docs.rs/quote/1.0/quote/trait.ToTokens.html
//! [`Span`]: https://docs.rs/proc-macro2/1.0/proc_macro2/struct.Span.html
#[cfg(feature = "parsing")]
pub(crate) use self::private::CustomToken;
use self::private::WithSpan;
#[cfg(feature = "parsing")]
use crate::buffer::Cursor;
Expand Down Expand Up @@ -134,7 +136,9 @@ pub trait Token: private::Sealed {
fn display() -> &'static str;
}

mod private {
pub(crate) mod private {
#[cfg(feature = "parsing")]
use crate::buffer::Cursor;
use proc_macro2::Span;

#[cfg(feature = "parsing")]
Expand All @@ -147,6 +151,14 @@ mod private {
pub struct WithSpan {
pub span: Span,
}

// Not public API.
#[doc(hidden)]
#[cfg(feature = "parsing")]
pub trait CustomToken {
fn peek(cursor: Cursor) -> bool;
fn display() -> &'static str;
}
}

#[cfg(feature = "parsing")]
Expand Down Expand Up @@ -218,14 +230,6 @@ impl_low_level_token!("punctuation token" Punct punct);
impl_low_level_token!("literal" Literal literal);
impl_low_level_token!("token" TokenTree token_tree);

// Not public API.
#[doc(hidden)]
#[cfg(feature = "parsing")]
pub trait CustomToken {
fn peek(cursor: Cursor) -> bool;
fn display() -> &'static str;
}

#[cfg(feature = "parsing")]
impl<T: CustomToken> private::Sealed for T {}

Expand Down

0 comments on commit eb1737d

Please sign in to comment.