Skip to content

Commit

Permalink
use built-in 'Default' trait
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Jan 13, 2024
1 parent 5609a45 commit 868c67d
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 102 deletions.
6 changes: 3 additions & 3 deletions capnp-futures/test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ mod tests {

let io = futures::future::join(done_reading, write_queue.map(|_| ()));

let mut m = capnp::message::Builder::new_default();
let mut m = capnp::message::Builder::default();
populate_address_book(m.init_root());

spawner.spawn_local(sender.send(m).map(|_| ())).unwrap();
Expand Down Expand Up @@ -153,7 +153,7 @@ mod tests {

#[test]
fn single_segment() {
fill_and_send_message(capnp::message::Builder::new_default());
fill_and_send_message(capnp::message::Builder::default());
}

#[test]
Expand All @@ -169,7 +169,7 @@ mod tests {
fn static_lifetime_not_required_funcs() {
let (mut write, mut read) = async_byte_channel::channel();
let _ = serialize::try_read_message(&mut read, message::ReaderOptions::default());
let _ = serialize::write_message(&mut write, message::Builder::new_default());
let _ = serialize::write_message(&mut write, message::Builder::default());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion capnp-rpc/src/broken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Request {
pub fn new(error: Error, _size_hint: Option<::capnp::MessageSize>) -> Self {
Self {
error,
message: ::capnp::message::Builder::new_default(),
message: ::capnp::message::Builder::default(),
cap_table: Vec::new(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions capnp-rpc/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct Results {
impl Results {
fn new(fulfiller: oneshot::Sender<Box<dyn ResultsDoneHook>>) -> Self {
Self {
message: Some(::capnp::message::Builder::new_default()),
message: Some(::capnp::message::Builder::default()),
cap_table: Vec::new(),
results_done_fulfiller: Some(fulfiller),
}
Expand Down Expand Up @@ -191,7 +191,7 @@ impl Request {
client: Box<dyn ClientHook>,
) -> Self {
Self {
message: message::Builder::new_default(),
message: message::Builder::default(),
cap_table: Vec::new(),
interface_id,
method_id,
Expand Down
2 changes: 1 addition & 1 deletion capnp-rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2209,7 +2209,7 @@ where
}
_ => {
self.variant = Some(ResultsVariant::LocallyRedirected(
::capnp::message::Builder::new_default(),
::capnp::message::Builder::default(),

Check warning on line 2212 in capnp-rpc/src/rpc.rs

View check run for this annotation

Codecov / codecov/patch

capnp-rpc/src/rpc.rs#L2212

Added line #L2212 was not covered by tests
Vec::new(),
));
}
Expand Down
2 changes: 1 addition & 1 deletion capnp-rpc/src/twoparty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ where
_first_segment_word_size: u32,
) -> Box<dyn crate::OutgoingMessage> {
Box::new(OutgoingMessage {
message: ::capnp::message::Builder::new_default(),
message: ::capnp::message::Builder::default(),
sender: self.inner.borrow().sender.clone(),
})
}
Expand Down
2 changes: 1 addition & 1 deletion capnp-rpc/test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ fn pipelining_return_null() {

#[test]
fn null_capability() {
let mut message = ::capnp::message::Builder::new_default();
let mut message = ::capnp::message::Builder::default();
let root: crate::test_capnp::test_all_types::Builder = message.get_root().unwrap();

// In capnproto-c++, this would return a BrokenCap. Here, it returns a decode error.
Expand Down
2 changes: 1 addition & 1 deletion capnp/fuzz/fuzzers/test_all_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn try_go(mut data: &[u8]) -> ::capnp::Result<()> {
root.total_size()?;
traverse(root)?;

let mut message = message::Builder::new_default();
let mut message = message::Builder::default();
message.set_root(root)?;

assert_equal(
Expand Down
2 changes: 1 addition & 1 deletion capnp/src/any_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
#[cfg(feature = "alloc")]
#[test]
fn init_clears_value() {
let mut message = crate::message::Builder::new_default();
let mut message = crate::message::Builder::default();
{
let root: crate::any_pointer::Builder = message.init_root();
let mut list: crate::primitive_list::Builder<u16> = root.initn_as(10);
Expand Down
27 changes: 24 additions & 3 deletions capnp/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
//! use capnp::message::{self, TypedBuilder, TypedReader};
//!
//! fn main() {
//! let mut builder = TypedBuilder::<simple_struct::Owned>::new_default();
//! let mut builder = TypedBuilder::<simple_struct::Owned>::default();
//! let mut builder_root = builder.init_root();
//! builder_root.set_x(10);
//! builder_root.set_y(20);
Expand Down Expand Up @@ -462,7 +462,7 @@ where

pub fn get_root_as_reader<'a, T: FromPointerReader<'a>>(&'a self) -> Result<T> {
if self.arena.is_empty() {
any_pointer::Reader::new(layout::PointerReader::new_default()).get_as()
any_pointer::Reader::new(layout::PointerReader::default()).get_as()
} else {
let (segment_start, _segment_len) = self.arena.get_segment(0)?;
let pointer_reader = layout::PointerReader::get_root(
Expand Down Expand Up @@ -571,8 +571,19 @@ impl<T> TypedBuilder<T, HeapAllocator>
where
T: Owned,
{
#[deprecated(since = "0.18.7", note = "use `TypedBuilder::default()`")]
pub fn new_default() -> Self {
Self::new(Builder::new_default())
Self::default()
}

Check warning on line 577 in capnp/src/message.rs

View check run for this annotation

Codecov / codecov/patch

capnp/src/message.rs#L576-L577

Added lines #L576 - L577 were not covered by tests
}

#[cfg(feature = "alloc")]
impl<T> Default for TypedBuilder<T, HeapAllocator>
where
T: Owned,
{
fn default() -> Self {
Self::new(Builder::default())
}
}

Expand Down Expand Up @@ -764,7 +775,17 @@ fn test_allocate_max() {
impl Builder<HeapAllocator> {
/// Constructs a new `message::Builder<HeapAllocator>` whose first segment has length
/// `SUGGESTED_FIRST_SEGMENT_WORDS`.
#[deprecated(since = "0.18.7", note = "use `Builder::default()`")]
pub fn new_default() -> Self {
Self::default()
}
}

#[cfg(feature = "alloc")]
impl Default for Builder<HeapAllocator> {
/// Constructs a new `message::Builder<HeapAllocator>` whose first segment has length
/// `SUGGESTED_FIRST_SEGMENT_WORDS`.
fn default() -> Self {
Self::new(HeapAllocator::new())
}
}
Expand Down
61 changes: 44 additions & 17 deletions capnp/src/private/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ mod wire_helpers {

if (*orig_ref).is_null() {
if default_value.is_null() || (*(default_value as *const WirePointer)).is_null() {
return Ok(ListBuilder::new_default(arena));
return Ok(ListBuilder::new(arena));

Check warning on line 1351 in capnp/src/private/layout.rs

View check run for this annotation

Codecov / codecov/patch

capnp/src/private/layout.rs#L1351

Added line #L1351 was not covered by tests
}
let (new_orig_ref_target, new_orig_ref, new_orig_segment_id) = copy_message(
arena,
Expand Down Expand Up @@ -1478,7 +1478,7 @@ mod wire_helpers {

if (*orig_ref).is_null() {
if default_value.is_null() || (*(default_value as *const WirePointer)).is_null() {
return Ok(ListBuilder::new_default(arena));
return Ok(ListBuilder::new(arena));

Check warning on line 1481 in capnp/src/private/layout.rs

View check run for this annotation

Codecov / codecov/patch

capnp/src/private/layout.rs#L1481

Added line #L1481 was not covered by tests
}
let (new_orig_ref_target, new_orig_ref, new_orig_segment_id) = copy_message(
arena,
Expand Down Expand Up @@ -2352,9 +2352,9 @@ mod wire_helpers {
) -> Result<StructReader<'a>> {
if (*reff).is_null() {
match default {
None => return Ok(StructReader::new_default()),
None => return Ok(StructReader::default()),
Some(d) if (*(d.as_ptr() as *const WirePointer)).is_null() => {
return Ok(StructReader::new_default())
return Ok(StructReader::default())

Check warning on line 2357 in capnp/src/private/layout.rs

View check run for this annotation

Codecov / codecov/patch

capnp/src/private/layout.rs#L2357

Added line #L2357 was not covered by tests
}
Some(d) => {
reff = d.as_ptr() as *const _;
Expand Down Expand Up @@ -2440,7 +2440,7 @@ mod wire_helpers {
) -> Result<ListReader<'_>> {
if (*reff).is_null() {
if default_value.is_null() || (*(default_value as *const WirePointer)).is_null() {
return Ok(ListReader::new_default());
return Ok(ListReader::default());
}
reff = default_value as *const _;
arena = &super::NULL_ARENA;
Expand Down Expand Up @@ -2842,16 +2842,23 @@ pub struct PointerReader<'a> {
nesting_limit: i32,
}

impl<'a> PointerReader<'a> {
pub fn new_default<'b>() -> PointerReader<'b> {
PointerReader {
impl<'a> Default for PointerReader<'a> {
fn default() -> Self {
Self {
arena: &NULL_ARENA,
segment_id: 0,
cap_table: Default::default(),
pointer: ptr::null(),
nesting_limit: 0x7fffffff,
}
}
}

impl<'a> PointerReader<'a> {
#[deprecated(since = "0.18.7", note = "use `PointerReader::default()`")]
pub fn new_default() -> Self {
Self::default()
}

Check warning on line 2861 in capnp/src/private/layout.rs

View check run for this annotation

Codecov / codecov/patch

capnp/src/private/layout.rs#L2859-L2861

Added lines #L2859 - L2861 were not covered by tests

pub fn get_root(
arena: &'a dyn ReaderArena,
Expand Down Expand Up @@ -3375,9 +3382,9 @@ pub struct StructReader<'a> {
nesting_limit: i32,
}

impl<'a> StructReader<'a> {
pub fn new_default<'b>() -> StructReader<'b> {
StructReader {
impl<'a> Default for StructReader<'a> {
fn default() -> Self {
Self {
arena: &NULL_ARENA,
segment_id: 0,
cap_table: Default::default(),
Expand All @@ -3388,6 +3395,13 @@ impl<'a> StructReader<'a> {
nesting_limit: 0x7fffffff,
}
}
}

impl<'a> StructReader<'a> {
#[deprecated(since = "0.18.7", note = "use `StructReader::default()`")]
pub fn new_default() -> Self {
Self::default()
}

Check warning on line 3404 in capnp/src/private/layout.rs

View check run for this annotation

Codecov / codecov/patch

capnp/src/private/layout.rs#L3402-L3404

Added lines #L3402 - L3404 were not covered by tests

pub fn imbue(&mut self, cap_table: CapTableReader) {
self.cap_table = cap_table
Expand Down Expand Up @@ -3479,7 +3493,7 @@ impl<'a> StructReader<'a> {
nesting_limit: self.nesting_limit,
}
} else {
PointerReader::new_default()
PointerReader::default()
}
}

Expand Down Expand Up @@ -3786,9 +3800,9 @@ pub struct ListReader<'a> {
element_size: ElementSize,
}

impl<'a> ListReader<'a> {
pub fn new_default<'b>() -> ListReader<'b> {
ListReader {
impl<'a> Default for ListReader<'a> {
fn default() -> Self {
Self {
arena: &NULL_ARENA,
segment_id: 0,
cap_table: Default::default(),
Expand All @@ -3801,6 +3815,13 @@ impl<'a> ListReader<'a> {
nesting_limit: 0x7fffffff,
}
}
}

impl<'a> ListReader<'a> {
#[deprecated(since = "0.18.7", note = "use `ListReader::default()`")]
pub fn new_default() -> Self {
Self::default()
}

Check warning on line 3824 in capnp/src/private/layout.rs

View check run for this annotation

Codecov / codecov/patch

capnp/src/private/layout.rs#L3822-L3824

Added lines #L3822 - L3824 were not covered by tests

pub fn imbue(&mut self, cap_table: CapTableReader) {
self.cap_table = cap_table
Expand Down Expand Up @@ -3996,8 +4017,14 @@ pub struct ListBuilder<'a> {

impl<'a> ListBuilder<'a> {
#[inline]
pub fn new_default(arena: &mut dyn BuilderArena) -> ListBuilder<'_> {
ListBuilder {
#[deprecated(since = "0.18.7", note = "use `ListBuilder::new()`")]
pub fn new_default(arena: &'a mut dyn BuilderArena) -> Self {
Self::new(arena)
}

Check warning on line 4023 in capnp/src/private/layout.rs

View check run for this annotation

Codecov / codecov/patch

capnp/src/private/layout.rs#L4021-L4023

Added lines #L4021 - L4023 were not covered by tests

#[inline]
pub fn new(arena: &'a mut dyn BuilderArena) -> Self {
Self {

Check warning on line 4027 in capnp/src/private/layout.rs

View check run for this annotation

Codecov / codecov/patch

capnp/src/private/layout.rs#L4026-L4027

Added lines #L4026 - L4027 were not covered by tests
arena,
segment_id: 0,
cap_table: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion capnp/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ pub mod test {
#[test]
fn compute_serialized_size() {
const LIST_LENGTH_IN_WORDS: u32 = 5;
let mut m = message::Builder::new_default();
let mut m = message::Builder::default();
{
let root: crate::any_pointer::Builder = m.init_root();
let _list_builder: crate::primitive_list::Builder<u64> =
Expand Down
18 changes: 9 additions & 9 deletions capnpc/test/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use capnp::{dynamic_list, dynamic_struct, dynamic_value};

#[test]
fn test_dynamic_reader() {
let mut builder = message::Builder::new_default();
let mut builder = message::Builder::default();
let root: test_all_types::Builder<'_> = builder.init_root();
let mut root: dynamic_value::Builder<'_> = root.into();

Expand All @@ -18,7 +18,7 @@ fn test_dynamic_reader() {

#[test]
fn test_dynamic_builder() {
let mut builder = message::Builder::new_default();
let mut builder = message::Builder::default();
let root: test_all_types::Builder<'_> = builder.init_root();
let mut root: dynamic_value::Builder<'_> = root.into();
test_util::dynamic_init_test_message(root.reborrow().downcast());
Expand All @@ -32,7 +32,7 @@ fn test_dynamic_builder() {
fn test_defaults() {
use crate::test_capnp::test_defaults;

let message = message::Builder::new_default();
let message = message::Builder::default();
let test_defaults = message
.get_root_as_reader::<test_defaults::Reader<'_>>()
.expect("get_root_as_reader()");
Expand All @@ -44,7 +44,7 @@ fn test_defaults() {
fn test_defaults_builder() {
use crate::test_capnp::test_defaults;

let mut message = message::Builder::new_default();
let mut message = message::Builder::default();
let test_defaults = message.get_root::<test_defaults::Builder<'_>>().unwrap();
let root: dynamic_value::Builder<'_> = test_defaults.into();
test_util::dynamic_check_test_message_builder(root.downcast());
Expand All @@ -54,7 +54,7 @@ fn test_defaults_builder() {
fn test_unions() {
use crate::test_capnp::test_union;
use capnp::{dynamic_struct, dynamic_value};
let mut message = message::Builder::new_default();
let mut message = message::Builder::default();
let mut root: test_union::Builder<'_> = message.init_root();
root.reborrow().get_union0().set_u0f1s32(1234567);
root.reborrow().get_union1().set_u1f1sp("foo");
Expand Down Expand Up @@ -150,7 +150,7 @@ fn test_unions() {
fn test_generics() {
use crate::test_capnp::{test_all_types, test_generics};
use capnp::text;
let mut message = message::Builder::new_default();
let mut message = message::Builder::default();
let root: test_generics::Builder<'_, test_all_types::Owned, text::Owned> = message.init_root();

let root: dynamic_value::Builder<'_> = root.into();
Expand Down Expand Up @@ -179,7 +179,7 @@ fn test_generics() {
#[test]
fn test_generic_annotation() -> ::capnp::Result<()> {
use crate::test_capnp::{test_generics, test_use_generics};
let mut message = message::Builder::new_default();
let mut message = message::Builder::default();
let root: test_use_generics::Builder<'_> = message.init_root();
let root: dynamic_value::Builder<'_> = root.into();
let root: dynamic_struct::Builder<'_> = root.downcast();
Expand All @@ -198,7 +198,7 @@ fn test_generic_annotation() -> ::capnp::Result<()> {
fn test_complex_list() {
use crate::test_capnp::test_complex_list;

let mut message = message::Builder::new_default();
let mut message = message::Builder::default();
let root = message.init_root::<test_complex_list::Builder<'_>>();
let root: dynamic_value::Builder<'_> = root.into();
let mut root: dynamic_struct::Builder<'_> = root.downcast();
Expand Down Expand Up @@ -237,7 +237,7 @@ fn test_complex_list() {
#[test]
fn test_stringify() {
use crate::test_capnp::{test_all_types, TestEnum};
let mut message = message::Builder::new_default();
let mut message = message::Builder::default();
let mut root: test_all_types::Builder<'_> = message.init_root();
root.set_int8_field(3);
root.set_enum_field(TestEnum::Bar);
Expand Down
Loading

0 comments on commit 868c67d

Please sign in to comment.