-
Notifications
You must be signed in to change notification settings - Fork 89
[Rust][Feat] Add match_any! for object-backed values #682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+245
−3
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8625f3e
feat(rust): add ordered match_any dispatch
tlopex 5bd5a65
refactor(rust): simplify match_any conversions and examples
tlopex 90b5fbb
test(rust): focus match_any coverage on practical cases
tlopex c808b57
refactor(rust): scope typed matching to objects
tlopex f45f6b0
fix(rust): exercise overlapping object guards
tlopex e851077
fix(rust): distinguish Any carriers from object patterns
tlopex 020906b
test(rust): focus match_any on lowering flow
tlopex 8ce070e
test(rust): use concrete object matchers
tlopex 47ea70c
refactor(rust): use TryInto for match_any arms
tlopex dae99f8
fix(rust): harden match_any integration
tlopex 926d5c5
refactor(rust): simplify crate path detection
tlopex 6edbe0f
refactor(rust): require Cargo crate name
tlopex dbea0c2
refactor(rust): inline crate name check
tlopex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| use proc_macro2::{Ident, Span, TokenStream}; | ||
| use quote::quote; | ||
| use syn::parse::{Parse, ParseStream}; | ||
| use syn::{braced, parenthesized, Expr, Pat, Path, Result, Token}; | ||
|
|
||
| use crate::utils::get_tvm_ffi_crate; | ||
|
|
||
| struct MatchAnyInput { | ||
| scrutinee: Expr, | ||
| arms: Vec<TypedArm>, | ||
| fallback: Expr, | ||
| } | ||
|
|
||
| struct TypedArm { | ||
| matcher: Path, | ||
| binding: Pat, | ||
| guard: Option<Expr>, | ||
| body: Expr, | ||
| } | ||
|
|
||
| impl Parse for MatchAnyInput { | ||
| fn parse(input: ParseStream<'_>) -> Result<Self> { | ||
| let scrutinee = input.call(Expr::parse_without_eager_brace)?; | ||
| let content; | ||
| braced!(content in input); | ||
|
|
||
| let mut arms = Vec::new(); | ||
| let mut fallback = None; | ||
| while !content.is_empty() { | ||
| if fallback.is_some() { | ||
| return Err(content.error("the `_` fallback must be the final arm")); | ||
| } | ||
|
|
||
| if content.peek(Token![_]) { | ||
| content.parse::<Token![_]>()?; | ||
| if content.peek(Token![if]) { | ||
| return Err(content.error("the `_` fallback cannot have a guard")); | ||
| } | ||
| content.parse::<Token![=>]>()?; | ||
| fallback = Some(content.parse::<Expr>()?); | ||
| } else { | ||
| let matcher = content.parse::<Path>()?; | ||
| let binding_content; | ||
| parenthesized!(binding_content in content); | ||
| let binding = binding_content.parse::<Pat>()?; | ||
| if !binding_content.is_empty() { | ||
| return Err(binding_content.error("expected one binding pattern")); | ||
| } | ||
| let guard = if content.peek(Token![if]) { | ||
| content.parse::<Token![if]>()?; | ||
| Some(content.parse::<Expr>()?) | ||
| } else { | ||
| None | ||
| }; | ||
| content.parse::<Token![=>]>()?; | ||
| let body = content.parse::<Expr>()?; | ||
| arms.push(TypedArm { | ||
| matcher, | ||
| binding, | ||
| guard, | ||
| body, | ||
| }); | ||
| } | ||
|
|
||
| if content.peek(Token![,]) { | ||
| content.parse::<Token![,]>()?; | ||
| } else if !content.is_empty() { | ||
| return Err(content.error("expected `,` between match_any! arms")); | ||
| } | ||
| } | ||
|
|
||
| let fallback = fallback | ||
| .ok_or_else(|| content.error("match_any! requires a final `_` fallback arm"))?; | ||
| Ok(Self { | ||
| scrutinee, | ||
| arms, | ||
| fallback, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| pub fn expand(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
| let input = syn::parse_macro_input!(input as MatchAnyInput); | ||
| expand_match_any(input).into() | ||
| } | ||
|
|
||
| fn expand_match_any(input: MatchAnyInput) -> TokenStream { | ||
| let tvm_ffi = get_tvm_ffi_crate(); | ||
| let span = Span::mixed_site(); | ||
| let source = Ident::new("__tvm_ffi_match_any_source", span); | ||
| let view = Ident::new("__tvm_ffi_match_any_view", span); | ||
| let rejected = Ident::new("__tvm_ffi_match_any_rejected", span); | ||
| let scrutinee = input.scrutinee; | ||
| let fallback = input.fallback; | ||
| let dispatch = input | ||
| .arms | ||
| .into_iter() | ||
| .rev() | ||
| .fold(quote!({ #fallback }), |next, arm| { | ||
| let matcher = arm.matcher; | ||
| let binding = arm.binding; | ||
| let body = arm.body; | ||
| let matched = if let Some(guard) = arm.guard { | ||
| quote!(::core::option::Option::Some(#binding) if #guard) | ||
| } else { | ||
| quote!(::core::option::Option::Some(#binding)) | ||
| }; | ||
|
|
||
| quote! { | ||
| match <#matcher as #tvm_ffi::AnyPattern>::try_match(#view) { | ||
| #matched => { #body }, | ||
| #rejected => { | ||
| ::core::mem::drop(#rejected); | ||
| #next | ||
| }, | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| quote! { | ||
| { | ||
| let #source = &(#scrutinee); | ||
| let #view = #tvm_ffi::AsAnyView::as_any_view(#source); | ||
| #dispatch | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| //! Runtime traits used by [`crate::match_any!`]. | ||
|
|
||
| use crate::{Any, AnyCompatible, AnyView}; | ||
|
|
||
| /// A value that can expose its contents as an erased [`AnyView`]. | ||
| /// | ||
| /// [`Any`], [`AnyView`], and every [`AnyCompatible`] value implement this | ||
| /// trait. | ||
| pub trait AsAnyView { | ||
| /// Borrow this value as an [`AnyView`]. | ||
| fn as_any_view(&self) -> AnyView<'_>; | ||
| } | ||
|
|
||
| impl<T: AnyCompatible> AsAnyView for T { | ||
|
tlopex marked this conversation as resolved.
Outdated
|
||
| #[inline] | ||
| fn as_any_view(&self) -> AnyView<'_> { | ||
| AnyView::from(self) | ||
| } | ||
| } | ||
|
|
||
| impl AsAnyView for Any { | ||
| #[inline] | ||
| fn as_any_view(&self) -> AnyView<'_> { | ||
| AnyView::from(self) | ||
| } | ||
| } | ||
|
|
||
| impl AsAnyView for AnyView<'_> { | ||
| #[inline] | ||
| fn as_any_view(&self) -> AnyView<'_> { | ||
| *self | ||
| } | ||
| } | ||
|
|
||
| /// A typed pattern accepted by [`crate::match_any!`]. | ||
| pub trait AnyPattern { | ||
| /// The binding produced by a successful match. | ||
| type Bound<'a>; | ||
|
|
||
| /// Try to match an erased value. | ||
| fn try_match<'a>(value: AnyView<'a>) -> Option<Self::Bound<'a>>; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.