Skip to content

Commit

Permalink
Added try_init for Ptr Field Builder for List<AnyStruct>
Browse files Browse the repository at this point in the history
  • Loading branch information
tewaro committed Aug 9, 2024
1 parent e7096f8 commit f033d2a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
18 changes: 12 additions & 6 deletions recapn/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,15 +1181,21 @@ impl<'b, 'p, T: Table> PtrFieldBuilder<'b, 'p, T, List<AnyStruct>> {
List::new(ptr)
}

#[inline]
pub fn try_init(self, size: StructSize, count: u32) -> Result<list::Builder<'b, AnyStruct, T>, TooManyElementsError> {
match ElementCount::new(count){
None => Err(TooManyElementsError(())),
Some(count) =>
Ok(List::new(self.into_raw_build_ptr()
.init_list(ElementSize::InlineComposite(size), count))),
}
}

#[inline]
pub fn init(self, size: StructSize, count: u32) -> list::Builder<'b, AnyStruct, T> {
let count = ElementCount::new(count).expect("too many elements for list");
List::new(
self.into_raw_build_ptr()
.init_list(ElementSize::InlineComposite(size), count),
)
self.try_init(size, count).expect("too many elements for list")
}

// TODO the rest of the accessors
}

Expand Down
1 change: 1 addition & 0 deletions recapn/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod ptr {
};
}

#[derive(Debug)]
pub struct TooManyElementsError(pub(crate) ());

pub type Reader<'a, V, T = rpc::Empty> = List<V, ptr::Reader<'a, T>>;
Expand Down

0 comments on commit f033d2a

Please sign in to comment.