From 9c339da955268502cc055d27e4f08e7fbf4203ee Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 14 Apr 2021 09:52:27 +0800 Subject: [PATCH] Simplify type system to leverage 'Sync' types These are generally available now that we use hyper's async APIs along with the most recent yup-hypermock. --- src/mako/api/api.rs.mako | 12 +++++------- src/mako/api/lib.rs.mako | 2 +- src/mako/api/lib/mbuild.mako | 11 +++++------ src/mako/lib/util.py | 10 +--------- src/rust/api/client.rs | 2 +- 5 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/mako/api/api.rs.mako b/src/mako/api/api.rs.mako index e7a37ea3da..fb1dddb863 100644 --- a/src/mako/api/api.rs.mako +++ b/src/mako/api/api.rs.mako @@ -5,7 +5,7 @@ <%namespace name="schema" file="lib/schema.mako"/>\ <% from util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment, - rb_type, hub_type, mangle_ident, hub_type_params_s, hub_type_bounds, + rb_type, hub_type, mangle_ident, hub_type_params_s, rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE, UNUSED_TYPE_MARKER, schema_markers) @@ -17,7 +17,6 @@ %>\ use std::collections::HashMap; use std::cell::RefCell; -use std::borrow::BorrowMut; use std::default::Default; use std::collections::BTreeMap; use serde_json as json; @@ -49,7 +48,7 @@ ${lib.scope_enum()} ${lib.hub_usage_example(c)}\ pub struct ${hub_type}${ht_params} { - client: RefCell, + client: hyper::Client, hyper::body::Body>, auth: oauth2::authenticator::Authenticator>, _user_agent: String, _base_url: String, @@ -58,12 +57,11 @@ pub struct ${hub_type}${ht_params} { impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> client::Hub for ${hub_type}${ht_params} {} -impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params} - where ${', '.join(hub_type_bounds())} { +impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params} { - pub fn new(client: C, authenticator: oauth2::authenticator::Authenticator>) -> ${hub_type}${ht_params} { + pub fn new(client: hyper::Client, hyper::body::Body>, authenticator: oauth2::authenticator::Authenticator>) -> ${hub_type}${ht_params} { ${hub_type} { - client: RefCell::new(client), + client, auth: authenticator, _user_agent: "${default_user_agent}".to_string(), _base_url: "${baseUrl}".to_string(), diff --git a/src/mako/api/lib.rs.mako b/src/mako/api/lib.rs.mako index e558e6fc6c..8daca47800 100644 --- a/src/mako/api/lib.rs.mako +++ b/src/mako/api/lib.rs.mako @@ -26,7 +26,7 @@ ${lib.docs(c)} <%namespace name="schema" file="lib/schema.mako"/>\ <% from util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment, - rb_type, hub_type, mangle_ident, hub_type_params_s, hub_type_bounds, + rb_type, hub_type, mangle_ident, hub_type_params_s, rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE, UNUSED_TYPE_MARKER, schema_markers) diff --git a/src/mako/api/lib/mbuild.mako b/src/mako/api/lib/mbuild.mako index 7415ccb4a8..5b8a5f8318 100644 --- a/src/mako/api/lib/mbuild.mako +++ b/src/mako/api/lib/mbuild.mako @@ -6,7 +6,7 @@ schema_to_required_property, rust_copy_value_s, is_required_property, hide_rust_doc_test, build_all_params, REQUEST_VALUE_PROPERTY_NAME, organize_params, indent_by, to_rust_type, rnd_arg_val_for_type, extract_parts, mb_type_params_s, - hub_type_params_s, method_media_params, enclose_in, mb_type_bounds, method_response, + hub_type_params_s, method_media_params, enclose_in, method_response, CALL_BUILDER_MARKERT_TRAIT, pass_through, markdown_rust_block, parts_from_params, DELEGATE_PROPERTY_NAME, struct_type_bounds_s, scope_url_to_variant, re_find_replacements, ADD_PARAM_FN, ADD_PARAM_MEDIA_EXAMPLE, upload_action_fn, METHODS_RESOURCE, @@ -132,7 +132,7 @@ pub struct ${ThisType} impl${mb_tparams} ${CALL_BUILDER_MARKERT_TRAIT} for ${ThisType} {} -impl${mb_tparams} ${ThisType} where ${', '.join(mb_type_bounds())} { +impl${mb_tparams} ${ThisType} { % if api.get('no_upload_prefix') is not None and ThisType.startswith(api.no_upload_prefix): ${self._action_fn(c, resource, method, m, params, request_value, parts, doit_without_upload = True)}\ % endif @@ -744,7 +744,7 @@ else { _ => (&mut request_value_reader as &mut dyn io::Read, (CONTENT_TYPE, json_mime_type.to_string())), }; % endif - let mut client = &mut *self.hub.client.borrow_mut(); + let client = &self.hub.client; dlg.pre_request(); let mut req_builder = hyper::Request::builder().method(${method_name_to_variant(m.httpMethod)}).uri(url.clone().into_string()) .header(USER_AGENT, self.hub._user_agent.clone())\ @@ -792,7 +792,7 @@ else { % endif ; - client.borrow_mut().request(request.unwrap()).await + client.request(request.unwrap()).await \ % if resumable_media_param: @@ -833,7 +833,6 @@ else { % if resumable_media_param: if protocol == "${resumable_media_param.protocol}" { ${READER_SEEK | indent_all_but_first_by(6)} - let mut client = &mut *self.hub.client.borrow_mut(); let upload_result = { let url_str = &res.headers().get("Location").expect("LOCATION header is part of protocol").to_str().unwrap(); if upload_url_from_server { @@ -841,7 +840,7 @@ else { } client::ResumableUploadHelper { - client: &mut client.borrow_mut(), + client: &self.hub.client, delegate: dlg, start_at: if upload_url_from_server { Some(0) } else { None }, auth: &${auth_call}, diff --git a/src/mako/lib/util.py b/src/mako/lib/util.py index 9aa730e848..adc8b4cef6 100644 --- a/src/mako/lib/util.py +++ b/src/mako/lib/util.py @@ -112,7 +112,7 @@ data_unit_multipliers = { '%': 1, } -HUB_TYPE_PARAMETERS = ('C',) +HUB_TYPE_PARAMETERS = () def items(p): if isinstance(p, dict): @@ -899,10 +899,6 @@ def _to_type_params_s(p): def hub_type_params_s(): return _to_type_params_s(HUB_TYPE_PARAMETERS) -# return a list of where statements to server as bounds for the hub. -def hub_type_bounds(): - return ['C: BorrowMut, hyper::body::Body>>'] - # Returns True if this API has particular authentication scopes to choose from def supports_scopes(auth): return bool(auth) and bool(auth.oauth2) @@ -922,10 +918,6 @@ def method_default_scope(m): # end try to find read-only default scope return default_scope -# return list of type bounds required by method builder -def mb_type_bounds(): - return hub_type_bounds() - _rb_type_params = ("'a", ) + HUB_TYPE_PARAMETERS diff --git a/src/rust/api/client.rs b/src/rust/api/client.rs index 84d05dfda6..96ae45f991 100644 --- a/src/rust/api/client.rs +++ b/src/rust/api/client.rs @@ -607,7 +607,7 @@ impl RangeResponseHeader { /// A utility type to perform a resumable upload from start to end. pub struct ResumableUploadHelper<'a, A: 'a> { - pub client: &'a mut hyper::client::Client< + pub client: &'a hyper::client::Client< hyper_rustls::HttpsConnector, hyper::body::Body, >,