mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-03 15:14:35 +00:00
SharedString and SharedVector should be Send
SharedString used to be Send, but this regress when we moved the implementation over to SharedVector. This regression was cought trying to compile CargoUI with master
This commit is contained in:
parent
dc7942fa86
commit
9440d3c003
2 changed files with 35 additions and 0 deletions
|
@ -86,6 +86,9 @@ pub struct SharedVector<T> {
|
||||||
inner: NonNull<SharedVectorInner<T>>,
|
inner: NonNull<SharedVectorInner<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Safety: We use atomic reference counting, and if T is Send, we can send the vector to another thread
|
||||||
|
unsafe impl<T: Send> Send for SharedVector<T> {}
|
||||||
|
|
||||||
impl<T> Drop for SharedVector<T> {
|
impl<T> Drop for SharedVector<T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
@ -224,6 +224,38 @@ fn simple_test() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn threading() {
|
||||||
|
let shared_cst = SharedString::from("Hello there!");
|
||||||
|
let shared_mtx = std::sync::Arc::new(std::sync::Mutex::new(SharedString::from("Shared:")));
|
||||||
|
let mut handles = vec![];
|
||||||
|
for _ in 0..20 {
|
||||||
|
let cst = shared_cst.clone();
|
||||||
|
let mtx = shared_mtx.clone();
|
||||||
|
handles.push(std::thread::spawn(move || {
|
||||||
|
assert_eq!(cst, "Hello there!");
|
||||||
|
let mut cst2 = cst.clone();
|
||||||
|
cst2.push_str(" ... or not?");
|
||||||
|
assert_eq!(cst2, "Hello there! ... or not?");
|
||||||
|
assert_eq!(cst.clone(), "Hello there!");
|
||||||
|
|
||||||
|
let shared = {
|
||||||
|
let mut lock = mtx.lock().unwrap();
|
||||||
|
assert!(lock.starts_with("Shared:"));
|
||||||
|
lock.push_str("!");
|
||||||
|
lock.clone()
|
||||||
|
};
|
||||||
|
assert!(shared.clone().starts_with("Shared:"));
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
for j in handles {
|
||||||
|
j.join().unwrap();
|
||||||
|
}
|
||||||
|
assert_eq!(shared_cst.clone(), "Hello there!");
|
||||||
|
assert_eq!(shared_mtx.lock().unwrap().as_str(), "Shared:!!!!!!!!!!!!!!!!!!!!");
|
||||||
|
// 20x"!"
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ffi")]
|
#[cfg(feature = "ffi")]
|
||||||
pub(crate) mod ffi {
|
pub(crate) mod ffi {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue