-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attachment api #613
Merged
Merged
Attachment api #613
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0359739
Add support for attachments
e10eff2
Merge branch 'attachment' into attachment-api
3e9eb0a
fix no_std, remove plural
4f7b886
Merge branch 'master' into attachment-api
091c84f
add attachment support in throughput tests
2371e83
stop encoding the number of elements at in Attachments, since we can'…
3e96a0a
provide AttachmentBuilder to have faster Attachment::insert API
62633bf
Merge branch 'attachment' into attachment-api
dd35540
remove accessors in builders, adapt examples
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,12 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
use alloc::{vec, vec::Vec}; | ||
use core::{ | ||
cmp::PartialEq, | ||
fmt, iter, | ||
ops::{Index, IndexMut}, | ||
ops::{Index, IndexMut, RangeBounds}, | ||
ptr, slice, | ||
}; | ||
|
||
|
@@ -112,6 +113,19 @@ impl<T> SingleOrVec<T> { | |
matches!(&self.0, SingleOrVecInner::Vec(v) if v.is_empty()) | ||
} | ||
|
||
fn vectorize(&mut self) -> &mut Vec<T> { | ||
if let SingleOrVecInner::Single(v) = &self.0 { | ||
unsafe { | ||
let v = core::ptr::read(v); | ||
core::ptr::write(&mut self.0, SingleOrVecInner::Vec(vec![v])) | ||
}; | ||
} | ||
let SingleOrVecInner::Vec(v) = &mut self.0 else { | ||
unsafe { core::hint::unreachable_unchecked() } | ||
}; | ||
v | ||
} | ||
|
||
pub fn get(&self, index: usize) -> Option<&T> { | ||
match &self.0 { | ||
SingleOrVecInner::Single(v) => (index == 0).then_some(v), | ||
|
@@ -139,6 +153,55 @@ impl<T> SingleOrVec<T> { | |
SingleOrVecInner::Vec(v) => v.last_mut(), | ||
} | ||
} | ||
pub fn drain<Range: RangeBounds<usize>>(&mut self, range: Range) -> Drain<T> { | ||
match &mut self.0 { | ||
this @ SingleOrVecInner::Single(_) if range.contains(&0) => Drain { | ||
inner: DrainInner::Single(this), | ||
}, | ||
SingleOrVecInner::Vec(vec) => Drain { | ||
inner: DrainInner::Vec(vec.drain(range)), | ||
}, | ||
_ => Drain { | ||
inner: DrainInner::Done, | ||
}, | ||
} | ||
} | ||
pub fn insert(&mut self, at: usize, value: T) { | ||
assert!(at <= self.len()); | ||
self.vectorize().insert(at, value); | ||
} | ||
} | ||
enum DrainInner<'a, T> { | ||
Vec(alloc::vec::Drain<'a, T>), | ||
Single(&'a mut SingleOrVecInner<T>), | ||
Done, | ||
} | ||
pub struct Drain<'a, T> { | ||
inner: DrainInner<'a, T>, | ||
} | ||
impl<'a, T> Iterator for Drain<'a, T> { | ||
type Item = T; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
match &mut self.inner { | ||
DrainInner::Vec(drain) => drain.next(), | ||
DrainInner::Single(inner) => match unsafe { core::ptr::read(*inner) } { | ||
SingleOrVecInner::Single(value) => unsafe { | ||
core::ptr::write(*inner, SingleOrVecInner::Vec(Vec::new())); | ||
Some(value) | ||
}, | ||
SingleOrVecInner::Vec(_) => None, | ||
}, | ||
_ => None, | ||
} | ||
} | ||
} | ||
impl<'a, T> Drop for Drain<'a, T> { | ||
fn drop(&mut self) { | ||
if let DrainInner::Single(_) = self.inner { | ||
self.next(); | ||
} | ||
} | ||
} | ||
|
||
impl<T> Default for SingleOrVec<T> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to check if
at
less than buffer sizeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, but computing
len
here is a bit expensive... that check can be done by checking forslice_index == usize::MAX
a bit later, adding to the todo list