raw-pointer

Casting a borrowed reference with a lifetime to a raw pointer in Rust

不打扰是莪最后的温柔 提交于 2021-01-28 11:51:24
问题 I am new to rust and am trying to wrap my head around lifetimes. Please consider the following code: use jni::JNIEnv; pub struct CameraAppEngine<'a> { _env: &'a JNIEnv<'a>, _width: i32, _height: i32 } impl<'a> CameraAppEngine<'a> { pub fn new(_env: &'a JNIEnv<'a>, _width: i32, _height: i32) -> CameraAppEngine { CameraAppEngine { _env, _width, _height } } pub fn env(&'a self) -> JNIEnv<'a> { JNIEnv::from_raw(self._env).unwrap() // error! } } The JNIEnv::from_raw method has a type signature of

How to get raw pointer from cusp library matrix format

喜夏-厌秋 提交于 2019-12-31 06:22:24
问题 I need to get raw pointer from cusp library matrix format. For example: cusp::coo_matrix<int,double,cusp::device_memory> A(3,3,4); A.values[0] = 1; A.row_indices[0] = 0; A.column_indices[0]= 1; A.values[1] = 2; A.row_indices[1] = 1; A.column_indices[1]= 0; A.values[2] = 3; A.row_indices[2] = 1; A.column_indices[2]= 1; A.values[3] = 4; A.row_indices[3] = 2; A.column_indices[3]= 2; How can I get the raw pointer to row_indices, column_indices and values arrays? I need to pass them to my kernels

Getting into smart pointers, how to deal with representing ownership?

只愿长相守 提交于 2019-12-10 16:04:13
问题 i've made a dynamic graph structure where both nodes and arcs are classes (i mean arcs are an actual instance in memory, they are not implied by an adjacency list of nodes to nodes). Each node has a list of pointers to the arcs it's connected to. Each arc has 2 pointers to the 2 nodes it's connecting. Deleting a node calls delete for each of its arcs. Each arc delete removes its pointer from the arcs lists in the 2 nodes it connects. Simplified: ~node() { while(arcs_list.size()) { delete arcs

How to make a struct field containing an Arc writable? [duplicate]

只愿长相守 提交于 2019-12-02 17:23:23
问题 This question already has an answer here : How do I share a mutable object between threads using Arc? (1 answer) Closed last year . I have a struct that somehow must be retrieved in the form of raw pointer. pub struct BufferData { /// Memory map for pixel data pub map: Arc<Box<memmap::MmapMut>>, pub otherdata: i32, } I need to write into its map field, so I dereference the raw pointer into struct, then try to write into its data field. But, I got the following error. error[E0596]: cannot

CString::new().unwrap().as_ptr() gives empty *const c_char

白昼怎懂夜的黑 提交于 2019-11-29 16:48:39
I have a C function that expects *const std::os::raw::c_char and I have done the following in Rust: use std::os::raw::c_char; use std::ffi::{CString, CStr}; extern crate libc; fn main() { let _test_str: *const c_char = CString::new("Hello World").unwrap().as_ptr(); let fmt: *const c_char = CString::new("%s\n").unwrap().as_ptr(); unsafe { libc::printf(fmt, _test_str); } unsafe { let slice = CStr::from_ptr(_test_str); println!("string buffer size without nul terminator: {}", slice.to_bytes().len()); } } However, I cannot get _test_str print out and the output of the above program is simply

CString::new().unwrap().as_ptr() gives empty *const c_char

℡╲_俬逩灬. 提交于 2019-11-28 10:43:54
问题 I have a C function that expects *const std::os::raw::c_char and I have done the following in Rust: use std::os::raw::c_char; use std::ffi::{CString, CStr}; extern crate libc; fn main() { let _test_str: *const c_char = CString::new("Hello World").unwrap().as_ptr(); let fmt: *const c_char = CString::new("%s\n").unwrap().as_ptr(); unsafe { libc::printf(fmt, _test_str); } unsafe { let slice = CStr::from_ptr(_test_str); println!("string buffer size without nul terminator: {}", slice.to_bytes()