Skip to content

Add SchemaWrite and SchemaRead implementations for Cell and RefCell#257

Open
adpthegreat wants to merge 14 commits intoanza-xyz:masterfrom
adpthegreat:add_cell_refcell
Open

Add SchemaWrite and SchemaRead implementations for Cell and RefCell#257
adpthegreat wants to merge 14 commits intoanza-xyz:masterfrom
adpthegreat:add_cell_refcell

Conversation

@adpthegreat
Copy link
Copy Markdown
Contributor

#110 reopened

@adpthegreat
Copy link
Copy Markdown
Contributor Author

@cpubot reopened

Comment on lines +2235 to +2243
const TYPE_META: TypeMeta = const {
match T::TYPE_META {
TypeMeta::Static { size, .. } => TypeMeta::Static {
size,
zero_copy: false,
},
TypeMeta::Dynamic => TypeMeta::Dynamic,
}
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe all TYPE_META declarations could use T::TYPE_META.keep_zero_copy(false)

Suggested change
const TYPE_META: TypeMeta = const {
match T::TYPE_META {
TypeMeta::Static { size, .. } => TypeMeta::Static {
size,
zero_copy: false,
},
TypeMeta::Dynamic => TypeMeta::Dynamic,
}
};
const TYPE_META: TypeMeta = T::TYPE_META.keep_zero_copy(false);


unsafe impl<C: ConfigCore, T> SchemaWrite<C> for Cell<T>
where
T: SchemaWrite<C>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe all these impls could include ?Sized on T


#[test]
fn test_cell_basic() {
proptest!(proptest_cfg(), |(value: (u32, f32, i64, f64, bool, u8))| {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the the straightforward implementation of Cell, it's not clear we're getting much value out of testing all these type variations.

The implementation is basically cell.get() and Cell::new. What's being done here is likely better accomplished in the fuzzer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess what we could test are the special cases like T != T::Dst / T::Src, T / T::Dst not being Sized


#[test]
fn test_cell_char_bincode_equivalence() {
proptest!(proptest_cfg(), |(cell: Cell<char>)| {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the above, what are we getting out of a dedicated Cell<char> test that we aren't getting with Cell<u32>?


#[test]
fn test_cell_arrays_bincode_equivalence() {
proptest!(proptest_cfg(), |(cell: Cell<[u32; 4]>)| {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the above, what are we getting out of a dedicated Cell<[u32; 4]> test that we aren't getting with Cell<u32>?


#[test]
fn test_refcell_basic() {
proptest!(proptest_cfg(), |(value: (RefCell<u32>, RefCell<f32>, RefCell<i64>, RefCell<f64>, RefCell<bool>, RefCell<u8>))| {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above


type Nested = RefCell<Vec<u64>>;

proptest!(proptest_cfg(), |(vec in proptest::collection::vec(any::<u64>(), 0..=5))| {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above

}

#[test]
fn test_refcell_with_struct() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as the above

Copy link
Copy Markdown
Contributor

@cpubot cpubot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few notes

@cpubot cpubot requested review from cpubot and kskalski March 24, 2026 23:21
fn size_of(src: &Self::Src) -> WriteResult<usize> {
let borrowed = src
.try_borrow()
.map_err(|_| crate::error::WriteError::Custom("RefCell already borrowed mutably"))?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to import WriteError in the file instead of using full qualified here

#[inline]
fn read(reader: impl Reader<'de>, dst: &mut MaybeUninit<Self::Dst>) -> ReadResult<()> {
let val = T::get(reader)?;
dst.write(RefCell::new(val));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to use T::Dst::new(val) here - makes the code more uniform across implementations and forces proper types without context-based (return value)) inference

@adpthegreat
Copy link
Copy Markdown
Contributor Author

adpthegreat commented Mar 26, 2026

Alright will make these changes as requested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants