-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
96 deletions.
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/// A trait containing all unsafe `Vec` functions used by this crate. | ||
pub(crate) trait UnsafeVec<T> { | ||
/// Push to a `Vec` without checking the capacity. | ||
unsafe fn unsafe_push(&mut self, item: T); | ||
|
||
/// Extend to a `Vec` without checking the capacity. | ||
unsafe fn unsafe_extend(&mut self, item: &[T]); | ||
} | ||
|
||
impl<T> UnsafeVec<T> for Vec<T> { | ||
unsafe fn unsafe_push(&mut self, item: T) { | ||
std::ptr::write(self.as_mut_ptr().add(self.len()), item); | ||
self.set_len(self.len().unchecked_add(1)); | ||
} | ||
|
||
unsafe fn unsafe_extend(&mut self, item: &[T]) { | ||
std::ptr::copy_nonoverlapping(item.as_ptr(), self.as_mut_ptr(), item.len()); | ||
self.set_len(self.len().unchecked_add(item.len())); | ||
} | ||
} |