Skip to content

Commit

Permalink
deprecate borrow() in favor of reborrow(), to eliminate ambiguity wit…
Browse files Browse the repository at this point in the history
…h std::borrow::Borrow
  • Loading branch information
dwrensha committed Mar 27, 2018
1 parent 9314483 commit aafa951
Show file tree
Hide file tree
Showing 20 changed files with 373 additions and 311 deletions.
44 changes: 22 additions & 22 deletions benchmark/carsales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ macro_rules! car_value_impl(
fn car_value (mut self) -> ::capnp::Result<u64> {
#![allow(unused_mut)]
let mut result : u64 = 0;
result += self.borrow().get_seats() as u64 * 200;
result += self.borrow().get_doors() as u64 * 350;
result += self.reborrow().get_seats() as u64 * 200;
result += self.reborrow().get_doors() as u64 * 350;

// Using an iterator here slows things down considerably.
// TODO: investigate why.
{
let mut wheels = try!(self.borrow().get_wheels());
let mut wheels = try!(self.reborrow().get_wheels());
for ii in 0..wheels.len() {
let mut wheel = wheels.borrow().get(ii);
result += wheel.borrow().get_diameter() as u64 * wheel.borrow().get_diameter() as u64;
result += if wheel.borrow().get_snow_tires() { 100 } else { 0 };
let mut wheel = wheels.reborrow().get(ii);
result += wheel.reborrow().get_diameter() as u64 * wheel.reborrow().get_diameter() as u64;
result += if wheel.reborrow().get_snow_tires() { 100 } else { 0 };
}
}

result += self.borrow().get_length() as u64 *
self.borrow().get_width() as u64 * self.borrow().get_height() as u64 / 50;
result += self.reborrow().get_length() as u64 *
self.reborrow().get_width() as u64 * self.reborrow().get_height() as u64 / 50;

{
let mut engine = try!(self.borrow().get_engine());
result += engine.borrow().get_horsepower() as u64 * 40;
if engine.borrow().get_uses_electric() {
if engine.borrow().get_uses_gas() {
let mut engine = try!(self.reborrow().get_engine());
result += engine.reborrow().get_horsepower() as u64 * 40;
if engine.reborrow().get_uses_electric() {
if engine.reborrow().get_uses_gas() {
//# hybrid
result += 5000;
} else {
Expand All @@ -63,12 +63,12 @@ macro_rules! car_value_impl(
}
}

result += if self.borrow().get_has_power_windows() { 100 } else { 0 };
result += if self.borrow().get_has_power_steering() { 200 } else { 0 };
result += if self.borrow().get_has_cruise_control() { 400 } else { 0 };
result += if self.borrow().get_has_nav_system() { 2000 } else { 0 };
result += if self.reborrow().get_has_power_windows() { 100 } else { 0 };
result += if self.reborrow().get_has_power_steering() { 200 } else { 0 };
result += if self.reborrow().get_has_cruise_control() { 400 } else { 0 };
result += if self.reborrow().get_has_nav_system() { 2000 } else { 0 };

result += self.borrow().get_cup_holders() as u64 * 25;
result += self.reborrow().get_cup_holders() as u64 * 25;

Ok(result)
}
Expand All @@ -93,9 +93,9 @@ pub fn random_car(rng: &mut FastRand, mut car: car::Builder) {
car.set_doors(2 + rng.next_less_than(3) as u8);

{
let mut wheels = car.borrow().init_wheels(4);
let mut wheels = car.reborrow().init_wheels(4);
for ii in 0..wheels.len() {
let mut wheel = wheels.borrow().get(ii);
let mut wheel = wheels.reborrow().get(ii);
wheel.set_diameter(25 + rng.next_less_than(15) as u16);
wheel.set_air_pressure((30.0 + rng.next_double(20.0)) as f32);
wheel.set_snow_tires(rng.next_less_than(16) == 0);
Expand All @@ -111,7 +111,7 @@ pub fn random_car(rng: &mut FastRand, mut car: car::Builder) {
car.set_weight(length as u32 * width as u32 * height as u32 / 200);

{
let mut engine = car.borrow().init_engine();
let mut engine = car.reborrow().init_engine();
engine.set_horsepower(100 * rng.next_less_than(400) as u16);
engine.set_cylinders(4 + 2 * rng.next_less_than(3) as u8);
engine.set_cc(800 + rng.next_less_than(10000));
Expand Down Expand Up @@ -140,8 +140,8 @@ impl ::TestCase for CarSales {
let mut result = 0;
let mut cars = request.init_cars(rng.next_less_than(200));
for ii in 0.. cars.len() {
let mut car = cars.borrow().get(ii);
random_car(rng, car.borrow());
let mut car = cars.reborrow().get(ii);
random_car(rng, car.reborrow());
result += car.car_value().unwrap();
}

Expand Down
6 changes: 3 additions & 3 deletions benchmark/catrank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ impl ::TestCase for CatRank {
let mut list = request.init_results(count);

for i in 0..count {
let mut result = list.borrow().get(i);
let mut result = list.reborrow().get(i);
result.set_score(1000.0 - i as f64);
let url_size = rng.next_less_than(100);

let url_prefix_length = URL_PREFIX.as_bytes().len();
{
let mut url = result.borrow().init_url(url_size + url_prefix_length as u32);
let mut url = result.reborrow().init_url(url_size + url_prefix_length as u32);

url.push_str(URL_PREFIX);
for _ in 0..url_size {
Expand Down Expand Up @@ -109,7 +109,7 @@ impl ::TestCase for CatRank {

let mut list = response.init_results(scored_results.len() as u32);
for i in 0..list.len() {
let mut item = list.borrow().get(i);
let mut item = list.reborrow().get(i);
let result = scored_results[i as usize];
item.set_score(result.score);
item.set_url(try!(result.result.get_url()));
Expand Down
8 changes: 4 additions & 4 deletions benchmark/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ fn make_expression(rng: &mut FastRand, mut exp: expression::Builder, depth : u32
let left : i32 =
if rng.next_less_than(8) < depth {
let tmp = (rng.next_less_than(128) + 1) as i32;
exp.borrow().get_left().set_value(tmp);
exp.reborrow().get_left().set_value(tmp);
tmp
} else {
make_expression(rng, exp.borrow().get_left().init_expression(), depth + 1)
make_expression(rng, exp.reborrow().get_left().init_expression(), depth + 1)
};

let right : i32 =
if rng.next_less_than(8) < depth {
let tmp = (rng.next_less_than(128) + 1) as i32;
exp.borrow().get_right().set_value(tmp);
exp.reborrow().get_right().set_value(tmp);
tmp
} else {
make_expression(rng, exp.borrow().get_right().init_expression(), depth + 1)
make_expression(rng, exp.reborrow().get_right().init_expression(), depth + 1)
};

match exp.get_op().unwrap() {
Expand Down
18 changes: 9 additions & 9 deletions capnp-futures/test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ mod tests {
fn populate_address_book(address_book: address_book::Builder) {
let mut people = address_book.init_people(2);
{
let mut alice = people.borrow().get(0);
let mut alice = people.reborrow().get(0);
alice.set_id(123);
alice.set_name("Alice");
alice.set_email("[email protected]");
{
let mut alice_phones = alice.borrow().init_phones(1);
alice_phones.borrow().get(0).set_number("555-1212");
alice_phones.borrow().get(0).set_type(person::phone_number::Type::Mobile);
let mut alice_phones = alice.reborrow().init_phones(1);
alice_phones.reborrow().get(0).set_number("555-1212");
alice_phones.reborrow().get(0).set_type(person::phone_number::Type::Mobile);
}
alice.get_employment().set_school("MIT");
}
Expand All @@ -54,11 +54,11 @@ mod tests {
bob.set_name("Bob");
bob.set_email("[email protected]");
{
let mut bob_phones = bob.borrow().init_phones(2);
bob_phones.borrow().get(0).set_number("555-4567");
bob_phones.borrow().get(0).set_type(person::phone_number::Type::Home);
bob_phones.borrow().get(1).set_number("555-7654");
bob_phones.borrow().get(1).set_type(person::phone_number::Type::Work);
let mut bob_phones = bob.reborrow().init_phones(2);
bob_phones.reborrow().get(0).set_number("555-4567");
bob_phones.reborrow().get(0).set_type(person::phone_number::Type::Home);
bob_phones.reborrow().get(1).set_number("555-7654");
bob_phones.reborrow().get(1).set_type(person::phone_number::Type::Work);
}
bob.get_employment().set_unemployed(());
}
Expand Down
30 changes: 15 additions & 15 deletions capnp-rpc/examples/calculator/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ fn try_main(args: Vec<String>) -> Result<(), ::capnp::Error> {
let mut subtract_call = request.get().init_expression().init_call();
subtract_call.set_function(subtract);
let mut subtract_params = subtract_call.init_params(2);
subtract_params.borrow().get(1).set_literal(67.0);
subtract_params.reborrow().get(1).set_literal(67.0);

let mut add_call = subtract_params.get(0).init_call();
add_call.set_function(add);
let mut add_params = add_call.init_params(2);
add_params.borrow().get(0).set_literal(123.0);
add_params.reborrow().get(0).set_literal(123.0);
add_params.get(1).set_literal(45.0);
}

Expand Down Expand Up @@ -181,8 +181,8 @@ fn try_main(args: Vec<String>) -> Result<(), ::capnp::Error> {
let mut multiply_call = request.get().init_expression().init_call();
multiply_call.set_function(multiply);
let mut multiply_params = multiply_call.init_params(2);
multiply_params.borrow().get(0).set_literal(4.0);
multiply_params.borrow().get(1).set_literal(6.0);
multiply_params.reborrow().get(0).set_literal(4.0);
multiply_params.reborrow().get(1).set_literal(6.0);
}

let multiply_result = request.send().pipeline.get_value();
Expand All @@ -193,8 +193,8 @@ fn try_main(args: Vec<String>) -> Result<(), ::capnp::Error> {
let mut add3_call = add3_request.get().init_expression().init_call();
add3_call.set_function(add.clone());
let mut add3_params = add3_call.init_params(2);
add3_params.borrow().get(0).set_previous_result(multiply_result.clone());
add3_params.borrow().get(1).set_literal(3.0);
add3_params.reborrow().get(0).set_previous_result(multiply_result.clone());
add3_params.reborrow().get(1).set_literal(3.0);
}

let add3_promise = add3_request.send().pipeline.get_value().read_request().send();
Expand All @@ -204,7 +204,7 @@ fn try_main(args: Vec<String>) -> Result<(), ::capnp::Error> {
let mut add5_call = add5_request.get().init_expression().init_call();
add5_call.set_function(add);
let mut add5_params = add5_call.init_params(2);
add5_params.borrow().get(0).set_previous_result(multiply_result);
add5_params.reborrow().get(0).set_previous_result(multiply_result);
add5_params.get(1).set_literal(5.0);
}

Expand Down Expand Up @@ -251,12 +251,12 @@ fn try_main(args: Vec<String>) -> Result<(), ::capnp::Error> {
let mut add_call = def_function_params.init_body().init_call();
add_call.set_function(add.clone());
let mut add_params = add_call.init_params(2);
add_params.borrow().get(1).set_parameter(1);
add_params.reborrow().get(1).set_parameter(1);

let mut multiply_call = add_params.get(0).init_call();
multiply_call.set_function(multiply.clone());
let mut multiply_params = multiply_call.init_params(2);
multiply_params.borrow().get(0).set_parameter(0);
multiply_params.reborrow().get(0).set_parameter(0);
multiply_params.get(1).set_literal(100.0);
}
}
Expand All @@ -272,17 +272,17 @@ fn try_main(args: Vec<String>) -> Result<(), ::capnp::Error> {
let mut multiply_call = def_function_params.init_body().init_call();
multiply_call.set_function(multiply);
let mut multiply_params = multiply_call.init_params(2);
multiply_params.borrow().get(1).set_literal(2.0);
multiply_params.reborrow().get(1).set_literal(2.0);

let mut f_call = multiply_params.get(0).init_call();
f_call.set_function(f.clone());
let mut f_params = f_call.init_params(2);
f_params.borrow().get(0).set_parameter(0);
f_params.reborrow().get(0).set_parameter(0);

let mut add_call = f_params.get(1).init_call();
add_call.set_function(add);
let mut add_params = add_call.init_params(2);
add_params.borrow().get(0).set_parameter(0);
add_params.reborrow().get(0).set_parameter(0);
add_params.get(1).set_literal(1.0);
}
}
Expand All @@ -294,7 +294,7 @@ fn try_main(args: Vec<String>) -> Result<(), ::capnp::Error> {
let mut f_call = f_eval_request.get().init_expression().init_call();
f_call.set_function(f);
let mut f_params = f_call.init_params(2);
f_params.borrow().get(0).set_literal(12.0);
f_params.reborrow().get(0).set_literal(12.0);
f_params.get(1).set_literal(34.0);
}
let f_eval_promise = f_eval_request.send().pipeline.get_value().read_request().send();
Expand Down Expand Up @@ -342,12 +342,12 @@ fn try_main(args: Vec<String>) -> Result<(), ::capnp::Error> {
pow_call.set_function(
calculator::function::ToClient::new(PowerFunction).from_server::<::capnp_rpc::Server>());
let mut pow_params = pow_call.init_params(2);
pow_params.borrow().get(0).set_literal(2.0);
pow_params.reborrow().get(0).set_literal(2.0);

let mut add_call = pow_params.get(1).init_call();
add_call.set_function(add);
let mut add_params = add_call.init_params(2);
add_params.borrow().get(0).set_literal(4.0);
add_params.reborrow().get(0).set_literal(4.0);
add_params.get(1).set_literal(5.0);
}

Expand Down
Loading

0 comments on commit aafa951

Please sign in to comment.