unsafe

Can I marshal a C-struct with a 2d array without using “unsafe”?

给你一囗甜甜゛ 提交于 2021-02-07 21:10:09
问题 I have a C DLL that I'm writing a C# interop class for. In the C DLL, one of the key methods fills a 2d structure; the structure is allocated and freed by helper methods, like so: // Simple Struct Definition -- Plain Old Data typedef struct MyPodStruct_s { double a; double b; } MyPodStruct; typedef struct My2dArray_s { MyPodStruct** arr; // allocated by Init2d; // array of arrays. // usage: arr[i][j] for i<n,j<m int n; int m; } My2dArray; void Init2d(My2dArray* s, int n, int m); void Free2d

Why are Python multiprocessing Pipe unsafe?

大憨熊 提交于 2021-02-07 05:36:07
问题 I don't understand why Pipes are said unsafe when there are multiple senders and receivers. How the following code can be turned into code using Queues if this is the case ? Queues don't throw EOFError when closed, so my processes can't stop. Should I send endlessly 'Poison' messages to tell them to stop (this way, i'm sure all my processes receive at least one poison) ? I would like to keep the pipe p1 open until I decide otherwise (here it's when I have sent the 10 messages). from

Calling an unsafe library object of from a different thread

别来无恙 提交于 2021-01-28 21:22:17
问题 I am using tikv/raft-rs library for implementing a consensus system. This library has a RawNode object which is a thread-unsafe object. We must execute some functions on this object periodically (example), hence I use a new thread for executing. Here are the constraints: I need to have this object on the main-thread doesn't have this object for accessing some its internal states. (e.g.: raw_node.is_leader ) This object must be accessed on a worker thread. Because of these constraints, this

Is undefined behavior possible in safe Rust?

我怕爱的太早我们不能终老 提交于 2021-01-28 03:21:26
问题 Is there any way to achieve undefined behavior in Rust without using unsafe ? Of course, such behavior can be wrapped by a third-party library in a "safe" function so let's assume we're using only the standard one. 回答1: Absolutely, but any such case is a bug with Rust or the standard libary. My favorite example is LLVM loop optimization can make safe programs crash, which actually occurs due to a poor interaction of Rust and LLVM semantics: pub fn oops() { (|| loop { drop(42) })() } Compiled

Is mutable accessor using a cast safe?

。_饼干妹妹 提交于 2021-01-27 05:50:46
问题 I am trying to understand the problems with duplicated code for & and &mut in getter-type functions. I'm trying to understand whether a particular solution to this problem using a cast inside an unsafe block would be safe. The following is an example of the problem. It is taken from the really nice tutorial Learning Rust With Entirely Too Many Linked Lists. type Link<T> = Option<Box<Node<T>>>; pub struct List<T> { head: Link<T>, } struct Node<T> { elem: T, next: Link<T>, } impl<T> List<T> { /

Is it legal to cast a struct to an array?

给你一囗甜甜゛ 提交于 2020-12-06 04:15:32
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

Is it legal to cast a struct to an array?

喜欢而已 提交于 2020-12-06 04:14:20
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

How to implement a `Future` / `Stream` that polls `async fn(&mut self)`?

↘锁芯ラ 提交于 2020-08-24 11:25:01
问题 I have the following struct struct Test; impl Test { async fn function(&mut self) {} } I want to implement an std::future::Future (well, actually futures::Stream , but it's basically the same) on Test , that would poll the function . My first try looked something like this impl Future for Test { type Output = (); fn poll(self: Pin<&mut self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match self.function() { Poll::Pending => Poll::Pending, Poll::Ready(_) => Poll::Ready(()), } } } Obviously

How to implement a `Future` / `Stream` that polls `async fn(&mut self)`?

安稳与你 提交于 2020-08-24 11:24:41
问题 I have the following struct struct Test; impl Test { async fn function(&mut self) {} } I want to implement an std::future::Future (well, actually futures::Stream , but it's basically the same) on Test , that would poll the function . My first try looked something like this impl Future for Test { type Output = (); fn poll(self: Pin<&mut self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match self.function() { Poll::Pending => Poll::Pending, Poll::Ready(_) => Poll::Ready(()), } } } Obviously