Skip to content

Commit

Permalink
Make zero-copy and ZeroCopyBuffer work with Vec<T> of length 0 (#…
Browse files Browse the repository at this point in the history
…48)

* Make `zero-copy` work with `Vec` of length 0

* Add test for zero-length `Vec<T>` and `[T;N]`

* Update into_dart.rs

* Respect cargo clippy
  • Loading branch information
temeddix authored Sep 22, 2023
1 parent c8bfb2d commit 6281240
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/into_dart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ fn vec_to_dart_native_external_typed_data<T>(
where
T: DartTypedDataTypeTrait,
{
if vec_from_rust.is_empty() {
let data = DartNativeTypedData {
ty: T::dart_typed_data_type(),
length: 0,
values: std::ptr::null_mut(),
};
return DartCObject {
ty: DartCObjectType::DartTypedData,
value: DartCObjectValue {
as_typed_data: data,
},
};
}

let mut vec = ManuallyDrop::new(vec_from_rust);
vec.shrink_to_fit();
let length = vec.len();
Expand Down
2 changes: 2 additions & 0 deletions tests/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn main() {
assert!(!isolate.post(vec![42.0f64; 100]));
assert!(!isolate.post(vec![true; 100]));
assert!(!isolate.post(vec![false; 100]));
assert!(!isolate.post(ZeroCopyBuffer(vec![0u8; 0])));
assert!(!isolate.post(ZeroCopyBuffer(vec![42i8; 100])));
assert!(!isolate.post(ZeroCopyBuffer(vec![42u8; 100])));
assert!(!isolate.post(ZeroCopyBuffer(vec![42i16; 100])));
Expand All @@ -45,6 +46,7 @@ fn main() {
assert!(!isolate.post([42.0f64; 100]));
assert!(!isolate.post([true; 100]));
assert!(!isolate.post([false; 100]));
assert!(!isolate.post(ZeroCopyBuffer([0u8; 0])));
assert!(!isolate.post(ZeroCopyBuffer([42i8; 100])));
assert!(!isolate.post(ZeroCopyBuffer([42u8; 100])));
assert!(!isolate.post(ZeroCopyBuffer([42i16; 100])));
Expand Down

0 comments on commit 6281240

Please sign in to comment.