Skip to content

Commit

Permalink
Add PointerMutability
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 7, 2024
1 parent c11a4db commit 5a12beb
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 3 deletions.
41 changes: 39 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,17 @@ ast_enum! {
}
}

#[cfg(feature = "full")]
ast_enum! {
/// Mutability of a raw pointer (`*const T`, `*mut T`), in which non-mutable
/// isn't the implicit default.
#[cfg_attr(docsrs, doc(cfg(feature = "full")))]
pub enum PointerMutability {
Const(Token![const]),
Mut(Token![mut]),
}
}

#[cfg(feature = "parsing")]
pub(crate) mod parsing {
#[cfg(feature = "full")]
Expand All @@ -1112,7 +1123,7 @@ pub(crate) mod parsing {
Arm, ExprArray, ExprAssign, ExprAsync, ExprAwait, ExprBlock, ExprBreak, ExprClosure,
ExprConst, ExprContinue, ExprForLoop, ExprIf, ExprInfer, ExprLet, ExprLoop, ExprMatch,
ExprRange, ExprRepeat, ExprReturn, ExprTry, ExprTryBlock, ExprUnsafe, ExprWhile, ExprYield,
Label, RangeLimits,
Label, PointerMutability, RangeLimits,
};
use crate::expr::{
Expr, ExprBinary, ExprCall, ExprCast, ExprField, ExprGroup, ExprIndex, ExprLit, ExprMacro,
Expand Down Expand Up @@ -2969,6 +2980,21 @@ pub(crate) mod parsing {
}
}

#[cfg(feature = "full")]
#[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
impl Parse for PointerMutability {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![const]) {
Ok(PointerMutability::Const(input.parse()?))
} else if lookahead.peek(Token![mut]) {
Ok(PointerMutability::Mut(input.parse()?))
} else {
Err(lookahead.error())
}
}
}

fn check_cast(input: ParseStream) -> Result<()> {
let kind = if input.peek(Token![.]) && !input.peek(Token![..]) {
if input.peek2(Token![await]) {
Expand Down Expand Up @@ -3004,7 +3030,7 @@ pub(crate) mod printing {
Arm, ExprArray, ExprAssign, ExprAsync, ExprAwait, ExprBlock, ExprBreak, ExprClosure,
ExprConst, ExprContinue, ExprForLoop, ExprIf, ExprInfer, ExprLet, ExprLoop, ExprMatch,
ExprRange, ExprRepeat, ExprReturn, ExprTry, ExprTryBlock, ExprUnsafe, ExprWhile, ExprYield,
Label, RangeLimits,
Label, PointerMutability, RangeLimits,
};
use crate::expr::{
Expr, ExprBinary, ExprCall, ExprCast, ExprField, ExprGroup, ExprIndex, ExprLit, ExprMacro,
Expand Down Expand Up @@ -3928,4 +3954,15 @@ pub(crate) mod printing {
}
}
}

#[cfg(feature = "full")]
#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for PointerMutability {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
PointerMutability::Const(const_token) => const_token.to_tokens(tokens),
PointerMutability::Mut(mut_token) => mut_token.to_tokens(tokens),
}
}
}
}
14 changes: 14 additions & 0 deletions src/gen/clone.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/gen/debug.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/gen/eq.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/gen/fold.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/gen/hash.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/gen/visit.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/gen/visit_mut.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ pub use crate::error::{Error, Result};
mod expr;
#[cfg(feature = "full")]
#[cfg_attr(docsrs, doc(cfg(feature = "full")))]
pub use crate::expr::{Arm, Label, RangeLimits};
pub use crate::expr::{Arm, Label, PointerMutability, RangeLimits};
#[cfg(any(feature = "full", feature = "derive"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))]
pub use crate::expr::{
Expand Down
20 changes: 20 additions & 0 deletions syn.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/debug/gen.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5a12beb

Please sign in to comment.