-
Notifications
You must be signed in to change notification settings - Fork 193
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
vec: remove code duplication due to VecView. #486
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e9f8bac
vec: remove code duplication due to VecView.
Dirbaio 400732b
vec: optimize for size a bit by preventing monomorphizing extend_from…
Dirbaio 1a08c7a
vec: generalize defmt, ufmt, ser impls to cover VecView.
Dirbaio bd03035
storage: expand docs for trait with an example of where it's used.
Dirbaio 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//! `Storage` trait defining how data is stored in a container. | ||
|
||
use core::borrow::{Borrow, BorrowMut}; | ||
|
||
pub(crate) trait SealedStorage { | ||
type Buffer<T>: ?Sized + Borrow<[T]> + BorrowMut<[T]>; | ||
} | ||
|
||
/// Trait defining how data for a container is stored. | ||
/// | ||
/// There's two implementations available: | ||
/// | ||
/// - [`OwnedStorage`]: stores the data in an array `[T; N]` whose size is known at compile time. | ||
/// - [`ViewStorage`]: stores the data in an unsized `[T]`. | ||
/// | ||
/// This allows containers to be generic over either sized or unsized storage. | ||
/// | ||
/// This trait is sealed, so you cannot implement it for your own types. You can only use | ||
/// the implementations provided by this crate. | ||
#[allow(private_bounds)] | ||
pub trait Storage: SealedStorage {} | ||
|
||
/// Implementation of [`Storage`] that stores the data in an array `[T; N]` whose size is known at compile time. | ||
pub enum OwnedStorage<const N: usize> {} | ||
impl<const N: usize> Storage for OwnedStorage<N> {} | ||
impl<const N: usize> SealedStorage for OwnedStorage<N> { | ||
type Buffer<T> = [T; N]; | ||
} | ||
|
||
/// Implementation of [`Storage`] that stores the data in an unsized `[T]`. | ||
pub enum ViewStorage {} | ||
impl Storage for ViewStorage {} | ||
impl SealedStorage for ViewStorage { | ||
type Buffer<T> = [T]; | ||
} |
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.
I would add an intra-doc link to the documentation of
VecView
andVec::as_view
to explain what these traits are used for.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.
done!