rust

Degrading performance of mongoldb document updates as record grows

☆樱花仙子☆ 提交于 2021-01-27 18:11:30
问题 I have an iOS application which sends batches of data to an API endpoint which stores the data into a mongodb database. My data is modeled like: { "_id" : ObjectId, "device_id" : Uuid, "rtfb_status": bool, "repetitions" : [ { "session_id" : Uuid, "set_id" : Uuid, "level" : String, "exercise" : String, "number" : i32, "rom" : f64, "duration" : f64, "time" : i64 }, ..., ], "imu_data": [ { "session_id": Uuid, "data": [ { "acc" : { "y" : f64, "z" : f64, "x" : f64, "time" : i64, }, "gyro" : { "y"

Degrading performance of mongoldb document updates as record grows

吃可爱长大的小学妹 提交于 2021-01-27 18:01:37
问题 I have an iOS application which sends batches of data to an API endpoint which stores the data into a mongodb database. My data is modeled like: { "_id" : ObjectId, "device_id" : Uuid, "rtfb_status": bool, "repetitions" : [ { "session_id" : Uuid, "set_id" : Uuid, "level" : String, "exercise" : String, "number" : i32, "rom" : f64, "duration" : f64, "time" : i64 }, ..., ], "imu_data": [ { "session_id": Uuid, "data": [ { "acc" : { "y" : f64, "z" : f64, "x" : f64, "time" : i64, }, "gyro" : { "y"

Can I use const with overloading operators in Rust?

戏子无情 提交于 2021-01-27 17:51:50
问题 In this code: #![allow(dead_code)] use std::ops::Add; struct Foo(i32); const X: i32 = 1; const Y: i32 = X + X; const A: Foo = Foo(1); const B: Foo = A + A; impl Add for Foo { type Output = Foo; fn add(self, rhs: Foo) -> Foo { Foo(self.0 + rhs.0) } } The compiler says: error[E0015]: calls in constants are limited to struct and enum constructors --> src/main.rs:8:16 | 8 | const B: Foo = A + A; | ^^^^^ | note: a limited form of compile-time function evaluation is available on a nightly compiler

Does &mut do anything when declaring a for loop variable?

前提是你 提交于 2021-01-27 17:41:19
问题 Consider the following (dumb) program: fn main() { let mut array = &mut [1u8, 2u8, 3u8]; for &mut value in array { } } It compiles and runs okay (though warns about unused variables/unnecessary mutability, as expected). But what does &mut do in the for statement? It doesn't seem to give you a mutable reference into the array, since trying to assign value = 0; results in the error: error[E0384]: re-assignment of immutable variable `value` Is &mut here a no-op then? 回答1: So there's a few

Method for safely moving all elements out of a generic array into a tuple with minimal overhead

旧巷老猫 提交于 2021-01-27 17:30:04
问题 In Rust, I want to move all the elements out of a generic fixed-width array so I may then move them individually. The elements may but don't necessarily implement Copy . I've come up with the following solution: struct Vec3<T> { underlying_array: [T; 3] } impl<T> Vec3<T> { fn into_tuple(self) -> (T, T, T) { let result = ( unsafe { mem::transmute_copy(&self.underlying_array[0]) }, unsafe { mem::transmute_copy(&self.underlying_array[1]) }, unsafe { mem::transmute_copy(&self.underlying_array[2])

“cannot find value `a` in this scope” in Rust macro

给你一囗甜甜゛ 提交于 2021-01-27 17:21:59
问题 I created macro for printing, using proc-macro-hack. Then this error occured though I already have defined a . Following is the code. On decl crate, proc_macro_expr_decl! { /// Function for printing to the standard output. /// /// First argument can be literal or not literal. gprint! => gprint_impl } On impl crate, use syn::{Expr, ExprTuple, parse_str}; use quote::ToTokens; fn _print_impl(input: &str, print_name: &str) -> String { let mut input_with_parens = String::with_capacity(input.len()

“cannot find value `a` in this scope” in Rust macro

一笑奈何 提交于 2021-01-27 17:17:46
问题 I created macro for printing, using proc-macro-hack. Then this error occured though I already have defined a . Following is the code. On decl crate, proc_macro_expr_decl! { /// Function for printing to the standard output. /// /// First argument can be literal or not literal. gprint! => gprint_impl } On impl crate, use syn::{Expr, ExprTuple, parse_str}; use quote::ToTokens; fn _print_impl(input: &str, print_name: &str) -> String { let mut input_with_parens = String::with_capacity(input.len()

Can I force a trait to be covariant?

跟風遠走 提交于 2021-01-27 16:57:11
问题 Thanks to @francis-gagné 's excellent answer to another question, I have a clearer view of how variance works. For example, a type containing a reference is covariant over its lifetime parameter, as demonstrated below. struct Foo<'a> (PhantomData<&'a str>); /// Foo is covariant over its lifetime parameter pub fn test_foo<'a:'b, 'b:'c, 'c>() { let fa: Foo<'a> = Foo(PhantomData); let fb: Foo<'b> = Foo(PhantomData); let fc: Foo<'c> = Foo(PhantomData); let v: Vec<Foo<'b>> = vec![fa, fb]; // fc is

How can I iteratively call Sha256::digest, passing the previous result to each subsequent call?

三世轮回 提交于 2021-01-27 16:54:41
问题 I'm trying to implement iterated SHA256. This works: use sha2::{Digest, Sha256}; // 0.8.2 fn main() { let preimage = [42; 80]; let hash = Sha256::digest(&Sha256::digest(&preimage)); } This doesn't: use sha2::{Digest, Sha256}; // 0.8.2 fn main() { let preimage = [42; 80]; let mut hash = preimage; for _ in 0..2 { hash = Sha256::digest(&hash); } } I get an error: error[E0308]: mismatched types --> src/main.rs:7:16 | 7 | hash = Sha256::digest(&hash); | ^^^^^^^^^^^^^^^^^^^^^ expected array `[u8;

Can I force a trait to be covariant?

女生的网名这么多〃 提交于 2021-01-27 16:51:27
问题 Thanks to @francis-gagné 's excellent answer to another question, I have a clearer view of how variance works. For example, a type containing a reference is covariant over its lifetime parameter, as demonstrated below. struct Foo<'a> (PhantomData<&'a str>); /// Foo is covariant over its lifetime parameter pub fn test_foo<'a:'b, 'b:'c, 'c>() { let fa: Foo<'a> = Foo(PhantomData); let fb: Foo<'b> = Foo(PhantomData); let fc: Foo<'c> = Foo(PhantomData); let v: Vec<Foo<'b>> = vec![fa, fb]; // fc is