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 Dec 3, 2022
1 parent 59efe94 commit 18fcb55
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 89 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 @@ -168,7 +168,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 @@ -2214,7 +2214,7 @@ where
}
_ => {
self.variant = Some(ResultsVariant::LocallyRedirected(
::capnp::message::Builder::new_default(),
::capnp::message::Builder::default(),
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 @@ -152,7 +152,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 @@ -354,7 +354,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 @@ -247,7 +247,7 @@ impl crate::capability::FromTypelessPipeline for Pipeline {

#[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
25 changes: 22 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 @@ -438,7 +438,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 @@ -526,12 +526,22 @@ where
message: Builder<A>,
}

impl<T> Default for TypedBuilder<T, HeapAllocator>
where
T: Owned,
{
fn default() -> Self {
Self::new(Builder::default())
}
}

impl<T> TypedBuilder<T, HeapAllocator>
where
T: Owned,
{
#[deprecated(since = "0.15.2", note = "use 'default()' instead")]
pub fn new_default() -> Self {
Self::new(Builder::new_default())
Self::new(Builder::default())
}
}

Expand Down Expand Up @@ -708,9 +718,18 @@ fn test_allocate_max() {
allocator.deallocate_segment(a3, s3, 0);
}

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())
}
}

impl Builder<HeapAllocator> {
/// Constructs a new `message::Builder<HeapAllocator>` whose first segment has length
/// `SUGGESTED_FIRST_SEGMENT_WORDS`.
#[deprecated(since = "0.15.2", note = "use 'default()' instead")]
pub fn new_default() -> Self {
Self::new(HeapAllocator::new())
}
Expand Down
64 changes: 46 additions & 18 deletions capnp/src/private/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,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());
return Ok(ListBuilder::default());
}
let (new_orig_ref_target, new_orig_ref, new_orig_segment_id) = copy_message(
arena,
Expand Down Expand Up @@ -1468,7 +1468,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());
return Ok(ListBuilder::default());
}
let (new_orig_ref_target, new_orig_ref, new_orig_segment_id) = copy_message(
arena,
Expand Down Expand Up @@ -2356,9 +2356,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())
}
Some(d) => {
reff = d.as_ptr() as *const _;
Expand Down Expand Up @@ -2446,7 +2446,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 @@ -2827,16 +2827,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: CapTableReader::Plain(ptr::null()),
pointer: ptr::null(),
nesting_limit: 0x7fffffff,
}
}
}

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

pub fn get_root(
arena: &'a dyn ReaderArena,
Expand Down Expand Up @@ -3348,9 +3355,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: CapTableReader::Plain(ptr::null()),
Expand All @@ -3361,6 +3368,13 @@ impl<'a> StructReader<'a> {
nesting_limit: 0x7fffffff,
}
}
}

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

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

Expand Down Expand Up @@ -3729,9 +3743,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: CapTableReader::Plain(ptr::null()),
Expand All @@ -3744,6 +3758,13 @@ impl<'a> ListReader<'a> {
nesting_limit: 0x7fffffff,
}
}
}

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

pub fn imbue(&mut self, cap_table: CapTableReader) {
self.cap_table = cap_table
Expand Down Expand Up @@ -3938,9 +3959,8 @@ pub struct ListBuilder<'a> {
element_size: ElementSize,
}

impl<'a> ListBuilder<'a> {
#[inline]
pub fn new_default<'b>() -> ListBuilder<'b> {
impl<'a> Default for ListBuilder<'a> {
fn default() -> Self {
ListBuilder {
arena: &NULL_ARENA,
segment_id: 0,
Expand All @@ -3953,6 +3973,14 @@ impl<'a> ListBuilder<'a> {
struct_pointer_count: 0,
}
}
}

impl<'a> ListBuilder<'a> {
#[inline]
#[deprecated(since = "0.15.2", note = "use 'default()' instead")]
pub fn new_default() -> Self {
Self::default()
}

pub fn into_reader(self) -> ListReader<'a> {
ListReader {
Expand Down
2 changes: 1 addition & 1 deletion capnp/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,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
Loading

0 comments on commit 18fcb55

Please sign in to comment.