我有一个结构,我想按值获取、变异然后返回。我还想改变它的泛型类型,因为我使用此状态静态地确保函数调用的正确顺序,以实现安全的 FFI(游乐场):
use core::marker::PhantomData;
struct State1 {}
struct State2 {}
struct Whatever {}
struct X<State> {
a: Whatever,
b: Whatever,
c: Whatever,
_d: PhantomData<State>,
}
impl<State> Drop for X<State> {
fn drop(&mut self) {}
}
fn f(x: X<State1>) -> X<State2> {
let X { a, b, c, _d } = x;
//mutate a, b and c
X {
a,
b,
c,
_d: PhantomData,
} // return new instance
}
Run Code Online (Sandbox Code Playgroud)
因为X实现了Drop,我得到:
use core::marker::PhantomData;
struct State1 …Run Code Online (Sandbox Code Playgroud) 我有一个类型的实例&T。我如何将其转换为&[T; 1]?类似于std::slice::from_ref,除了它应该返回一个数组,而不是一个切片。
在本教程中,它展示了以下导出 C 函数的示例
./emcc tests/hello_function.cpp -o function.html -s EXPORTED_FUNCTIONS='["_int_sqrt"]' -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]'
Run Code Online (Sandbox Code Playgroud)
我想做同样的事情,除了我像这样使用 CMake
cd bin
emcmake cmake ../src
emmake make
Run Code Online (Sandbox Code Playgroud)
emmake 中指定的规范方式是什么-s?我应该添加它来CMakeLists.txt喜欢吗
set(EXPORTED_FUNCTIONS '["_int_sqrt"]')
Run Code Online (Sandbox Code Playgroud)
或者做类似的事情?
Display我正在尝试根据参数实现不同的格式。print_json()类似于和 的东西
print_pretty()。我当然可以将其实现为返回字符串的函数print_json(&self)->String,但我想知道是否可以使用print_json(&self,f: &mut Formatter<'_>) -> std::fmt::Resultandprint_pretty(&self,f: &mut Formatter<'_>) -> std::fmt::Result来代替。然后我可以根据用例调用其中一个函数。但如何Formatter直接获取实例呢?理想情况下我想做类似的事情
let mut string = String::new();
my_object.print_pretty(&mut string);
return string;
Run Code Online (Sandbox Code Playgroud) 我可以foo为数组定义一个关联函数,如下所示:
pub trait T {
fn foo();
}
impl<X> T for [X; 2] {
fn foo() { panic!("Whatever") }
}
Run Code Online (Sandbox Code Playgroud)
但现在我该如何调用这个函数呢?我注意到类似的语法[usize;2]::foo()是无效的。
我试图了解整个 L1/L2 冲洗是如何工作的。假设我有一个像这样的计算着色器
layout(std430, set = 0, binding = 2) buffer Particles{
Particle particles[];
};
layout(std430, set = 0, binding = 4) buffer Constraints{
Constraint constraints[];
};
void main(){
const uint gID = gl_GlobalInvocationID.x;
for (int pass=0;pass<GAUSS_SEIDEL_PASSES;pass++){
// first query the constraint, which contains particle_id_1 and particle_id_1
const Constraint c = constraints[gID*GAUSS_SEIDEL_PASSES+pass];
// read newest positions
vec3 position1 = particles[c.particle_id_1].position;
vec3 position2 = particles[c.particle_id_2].position;
// modify position1 and position2
position1 += something;
position2 -= something;
// update positions
particles[c.particle_id_1].position = …Run Code Online (Sandbox Code Playgroud) 我有一个简单的类,注释为#[pyclass]
#[pyclass]
pub struct A {
...
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个形式的函数
fn f(slf: Py<Self>) -> PyObject{
//... some code here
let output = A{...};
output.to_object() // Error: method `to_object` not found for this
}
Run Code Online (Sandbox Code Playgroud)
我应该用一些东西注释我的结构以使其派生pyo3::ToPyObject吗?
假设我有以下结构
trait T{}
struct A<X:T>{
...
}
Run Code Online (Sandbox Code Playgroud)
我想知道这样的事情是否可能
Box<A<dyn T>>
Run Code Online (Sandbox Code Playgroud)
目前我收到错误
trait T{}
struct A<X:T>{
...
}
Run Code Online (Sandbox Code Playgroud)
但是当我添加
trait T:Sized{}
Run Code Online (Sandbox Code Playgroud)
我明白了
Box<A<dyn T>>
Run Code Online (Sandbox Code Playgroud)