-
Notifications
You must be signed in to change notification settings - Fork 180
RUST-2103 Update the rest of actions to document options #1272
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
abr-egn
merged 33 commits into
mongodb:main
from
abr-egn:RUST-2103/the-return-of-action-doc
Jan 2, 2025
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
07bcc50
create_index
abr-egn cb77079
create_data_key
abr-egn cafdf97
wip: rework option_setter2 arg parsing
abr-egn 414242c
clean up parsing
abr-egn 810d99c
split up
abr-egn f935c50
untested skip
abr-egn 54f44cc
encrypt (but its not right)
abr-egn 57b038b
split macros
abr-egn 42b4e2b
fix encrypt doc
abr-egn b3ad8d9
create_encrypted_collection
abr-egn 2361b3a
delete
abr-egn 5c282c5
distinct
abr-egn c139bbb
drop_index
abr-egn 895a39d
drop
abr-egn aba8401
find_and_modify
abr-egn 71b77b3
gridfs
abr-egn e6a5bb9
insert_mant
abr-egn a44b6c9
insert_one
abr-egn b52b87f
list_collections
abr-egn 47e245e
list_databases
abr-egn e6c28e5
list_indexes
abr-egn 63cf7d4
drop _setters suffix
abr-egn d1a9d6a
replace_one
abr-egn 6b6e238
run_command
abr-egn 7cb7c7b
search_index
abr-egn 08decda
session
abr-egn f7313df
shutdown
abr-egn fcdabb3
transaction
abr-egn 440d33f
update
abr-egn 438bd2f
watch
abr-egn 1596abe
drop old option_setters
abr-egn 4c994ff
rename
abr-egn 7ed6a86
fix imports
abr-egn 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
extern crate proc_macro; | ||
|
||
use quote::quote; | ||
use syn::{ | ||
braced, | ||
parenthesized, | ||
parse::{Parse, ParseStream}, | ||
parse_macro_input, | ||
parse_quote_spanned, | ||
spanned::Spanned, | ||
Block, | ||
Generics, | ||
Ident, | ||
Lifetime, | ||
Token, | ||
Type, | ||
}; | ||
|
||
use crate::parse_name; | ||
|
||
pub(crate) fn action_impl( | ||
attrs: proc_macro::TokenStream, | ||
input: proc_macro::TokenStream, | ||
) -> proc_macro::TokenStream { | ||
let ActionImplAttrs { sync_type } = parse_macro_input!(attrs as ActionImplAttrs); | ||
let ActionImpl { | ||
generics, | ||
lifetime, | ||
action, | ||
future_name, | ||
exec_self_mut, | ||
exec_output, | ||
exec_body, | ||
} = parse_macro_input!(input as ActionImpl); | ||
|
||
let mut unbounded_generics = generics.clone(); | ||
for lt in unbounded_generics.lifetimes_mut() { | ||
lt.bounds.clear(); | ||
} | ||
for ty in unbounded_generics.type_params_mut() { | ||
ty.bounds.clear(); | ||
} | ||
|
||
let sync_run = if let Some(sync_type) = sync_type { | ||
quote! { | ||
/// Synchronously execute this action. | ||
pub fn run(self) -> Result<#sync_type> { | ||
crate::sync::TOKIO_RUNTIME.block_on(std::future::IntoFuture::into_future(self)).map(<#sync_type>::new) | ||
} | ||
} | ||
} else { | ||
quote! { | ||
/// Synchronously execute this action. | ||
pub fn run(self) -> #exec_output { | ||
crate::sync::TOKIO_RUNTIME.block_on(std::future::IntoFuture::into_future(self)) | ||
} | ||
} | ||
}; | ||
|
||
quote! { | ||
impl #generics crate::action::private::Sealed for #action { } | ||
|
||
impl #generics crate::action::Action for #action { } | ||
|
||
impl #generics std::future::IntoFuture for #action { | ||
type Output = #exec_output; | ||
type IntoFuture = #future_name #unbounded_generics; | ||
|
||
fn into_future(#exec_self_mut self) -> Self::IntoFuture { | ||
#future_name (Box::pin(async move { | ||
#exec_body | ||
})) | ||
} | ||
} | ||
|
||
pub struct #future_name #generics (crate::BoxFuture<#lifetime, #exec_output>); | ||
|
||
impl #generics std::future::Future for #future_name #unbounded_generics { | ||
type Output = #exec_output; | ||
|
||
fn poll(mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> { | ||
self.0.as_mut().poll(cx) | ||
} | ||
} | ||
|
||
#[cfg(feature = "sync")] | ||
impl #generics #action { | ||
#sync_run | ||
} | ||
}.into() | ||
} | ||
|
||
// impl<generics> Action for ActionType { | ||
// type Future = FutureName; | ||
// async fn execute([mut] self) -> OutType { <exec body> } | ||
// [SyncWrap] | ||
// } | ||
struct ActionImpl { | ||
generics: Generics, | ||
lifetime: Lifetime, | ||
action: Type, | ||
future_name: Ident, | ||
exec_self_mut: Option<Token![mut]>, | ||
exec_output: Type, | ||
exec_body: Block, | ||
} | ||
|
||
impl Parse for ActionImpl { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
// impl<generics> Action for ActionType | ||
input.parse::<Token![impl]>()?; | ||
let generics: Generics = input.parse()?; | ||
let mut lifetime = None; | ||
for lt in generics.lifetimes() { | ||
if lifetime.is_some() { | ||
return Err(input.error("only one lifetime argument permitted")); | ||
} | ||
lifetime = Some(lt); | ||
} | ||
let lifetime = match lifetime { | ||
Some(lt) => lt.lifetime.clone(), | ||
None => parse_quote_spanned! { generics.span() => 'static }, | ||
}; | ||
parse_name(input, "Action")?; | ||
input.parse::<Token![for]>()?; | ||
let action = input.parse()?; | ||
|
||
let impl_body; | ||
braced!(impl_body in input); | ||
|
||
// type Future = FutureName; | ||
impl_body.parse::<Token![type]>()?; | ||
parse_name(&impl_body, "Future")?; | ||
impl_body.parse::<Token![=]>()?; | ||
let future_name = impl_body.parse()?; | ||
impl_body.parse::<Token![;]>()?; | ||
|
||
// async fn execute([mut] self) -> OutType { <exec body> } | ||
impl_body.parse::<Token![async]>()?; | ||
impl_body.parse::<Token![fn]>()?; | ||
parse_name(&impl_body, "execute")?; | ||
let exec_args; | ||
parenthesized!(exec_args in impl_body); | ||
let exec_self_mut = exec_args.parse()?; | ||
exec_args.parse::<Token![self]>()?; | ||
if !exec_args.is_empty() { | ||
return Err(exec_args.error("unexpected token")); | ||
} | ||
impl_body.parse::<Token![->]>()?; | ||
let exec_output = impl_body.parse()?; | ||
let exec_body = impl_body.parse()?; | ||
|
||
if !impl_body.is_empty() { | ||
return Err(exec_args.error("unexpected token")); | ||
} | ||
|
||
Ok(ActionImpl { | ||
generics, | ||
lifetime, | ||
action, | ||
future_name, | ||
exec_self_mut, | ||
exec_output, | ||
exec_body, | ||
}) | ||
} | ||
} | ||
|
||
struct ActionImplAttrs { | ||
sync_type: Option<Type>, | ||
} | ||
|
||
impl Parse for ActionImplAttrs { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let mut out = Self { sync_type: None }; | ||
if input.is_empty() { | ||
return Ok(out); | ||
} | ||
|
||
parse_name(input, "sync")?; | ||
input.parse::<Token![=]>()?; | ||
out.sync_type = Some(input.parse()?); | ||
Ok(out) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing in here changed, just moved to its own file.