fix(mbuild): setters now copy copyables

Previously, they would take everything as reference and clone
everything unconditionally. Now we do it only as we need to do it,
no extra work incurred.
This commit is contained in:
Sebastian Thiel
2015-03-04 21:13:04 +01:00
parent 693b5c8f6a
commit 452b658c27
4 changed files with 118 additions and 109 deletions

View File

@@ -2,7 +2,7 @@
from util import (put_and, rust_test_fn_invisible, rust_doc_test_norun, rust_doc_comment,
rb_type, mb_type, singular, hub_type, to_fqan, indent_all_but_first_by,
method_params, activity_rust_type, mangle_ident, activity_input_type, get_word,
split_camelcase_s, property, TREF)
split_camelcase_s, property, is_pod_property, TREF)
%>\
<%namespace name="util" file="util.mako"/>\
<%namespace name="lib" file="lib.mako"/>\
@@ -37,7 +37,7 @@ ${lib.test_hub(hub_type_name, comments=False)}\
// Usually you wouldn't bind this to a variable, but keep calling methods
// to setup your call.
// TODO: figoure out actual arguments ...
// let mb = hub.${resource}().${method}(...);
// let mb = hub.${resource}().${mangle_ident(method)}(...);
// Finally, execute your call and process the result
// TODO: comment in once args are properly setup !
@@ -88,6 +88,13 @@ ${self._setter(resource, method, m, p, ThisType, c)}\
if not (m.get('request') and m.get('response')):
return False
return m.request.get(TREF, 'first') == m.response.get(TREF, 'second')
value_name = 'new_value'
new_value_copied = value_name + '.clone()'
if InType == '&str':
new_value_copied = value_name + '.to_string()'
elif is_pod_property(p):
new_value_copied = value_name
%>\
/// Sets the *${split_camelcase_s(p.name)}* ${get_word(p, 'location')}property to the given value.
///
@@ -105,12 +112,8 @@ ${self._setter(resource, method, m, p, ThisType, c)}\
% if 'description' in p:
${p.description | rust_doc_comment, indent_all_but_first_by(1)}
% endif
pub fn ${mangle_ident(p.name)}(&mut self, new_value: ${InType}) -> &mut ${ThisType} {
% if InType == '&str':
self.${property(p.name)} = Some(new_value.to_string());
% else:
self.${property(p.name)} = Some(new_value.clone());
% endif
pub fn ${mangle_ident(p.name)}(&mut self, ${value_name}: ${InType}) -> &mut ${ThisType} {
self.${property(p.name)} = Some(${new_value_copied});
return self;
}
</%def>

View File

@@ -28,7 +28,7 @@ ${util.test_prelude()}\
${lib.test_hub(hub_type_name, comments=False)}\
// Usually you wouldn't bind this to a variable, but keep calling *MethodBuilders*
// like ${put_and(sorted('`%s(...)`' % f for f in c.rta_map[resource]))}
// like ${put_and(sorted('`%s(...)`' % mangle_ident(f) for f in c.rta_map[resource]))}
// to build up your call.
let rb = hub.${mangle_ident(resource)}();
</%block>
@@ -56,7 +56,7 @@ impl<'a, C, NC, A> ${ThisType} {
///
${m.description | rust_doc_comment, indent_all_but_first_by(1)}
% endif
fn ${mangle_ident(a)}(&self) -> ${RType}<'a, C, NC, A> {
pub fn ${mangle_ident(a)}(&self) -> ${RType}<'a, C, NC, A> {
${RType} {
hub: self.hub,
% for p in method_params(m):

View File

@@ -219,8 +219,14 @@ def activity_input_type(p):
n = activity_rust_type(p, allow_optionals=False)
if n == 'String':
n = 'str'
# pods are copied anyway
elif is_pod_property(p):
return n
return '&%s' % n
def is_pod_property(p):
return 'format' in p or p.type == 'boolean'
# return an iterator yielding fake-schemas that identify a nested type
def iter_nested_types(schemas):
for s in schemas.values():