-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rust, python): fix slice in streaming (#5854)
- Loading branch information
Showing
9 changed files
with
124 additions
and
47 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
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,47 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
use std::ptr::NonNull; | ||
use std::sync::atomic::AtomicUsize; | ||
|
||
#[derive(Clone)] | ||
/// A utility to create a sharable counter | ||
/// This does not implement drop as the user | ||
/// needs to decide when to drop it. Which is likely | ||
/// the moment the last thread is finished. | ||
pub struct SyncCounter { | ||
count: NonNull<AtomicUsize>, | ||
} | ||
|
||
impl SyncCounter { | ||
pub fn new(value: usize) -> Self { | ||
let count = Box::new(AtomicUsize::new(value)); | ||
let ptr = Box::leak(count); | ||
|
||
// leak a box so that we get a pointer that remains valid until we drop Self | ||
let count = unsafe { NonNull::new_unchecked(ptr) }; | ||
SyncCounter { count } | ||
} | ||
|
||
/// # Safety | ||
/// This will deref the pointer and after this all autoderef will be invalid. | ||
pub unsafe fn manual_drop(&mut self) { | ||
// recreate the box and drop it | ||
unsafe { Box::from_raw(self.count.as_ptr()) }; | ||
} | ||
} | ||
|
||
impl Deref for SyncCounter { | ||
type Target = AtomicUsize; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
unsafe { self.count.as_ref() } | ||
} | ||
} | ||
|
||
impl DerefMut for SyncCounter { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
unsafe { self.count.as_mut() } | ||
} | ||
} | ||
|
||
unsafe impl Sync for SyncCounter {} | ||
unsafe impl Send for SyncCounter {} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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