rust

Shared mutable state in Hyper

廉价感情. 提交于 2020-03-25 04:40:00
问题 I'm trying to create a counter in a Hyper web server that counts the number of requests it has received. I'm using a Arc<Mutex<u64>> to hold onto count. However, I haven't been able to figure out the right combination of move and .clone() to satisfy the types of the closures. Here's some code that compiles, but resets the counter on each request: extern crate hyper; use hyper::rt::Future; use hyper::service::service_fn_ok; use hyper::{Body, Response, Server}; use std::sync::{Arc, Mutex}; fn

Shared mutable state in Hyper

拜拜、爱过 提交于 2020-03-25 04:39:09
问题 I'm trying to create a counter in a Hyper web server that counts the number of requests it has received. I'm using a Arc<Mutex<u64>> to hold onto count. However, I haven't been able to figure out the right combination of move and .clone() to satisfy the types of the closures. Here's some code that compiles, but resets the counter on each request: extern crate hyper; use hyper::rt::Future; use hyper::service::service_fn_ok; use hyper::{Body, Response, Server}; use std::sync::{Arc, Mutex}; fn

What's the difference between using the return statement and omitting the semicolon in Rust?

泄露秘密 提交于 2020-03-24 18:13:52
问题 I'm writing a function that returns a serde_json::Value upon success (and failure). Previously in Rust I have been omitting the semicolon to return data from a function, like in the code example below: use serde_json::{Result, Value}; use core::result::Result as ResultCore; fn returning_function() -> ResultCore<Value, Value> { let data = r#" { "status": "ok", "response": { "data": "secret message!" } } "#; match str_to_json(data) { Ok(json_data) => match json_data["status"].as_str() { Some

TiKV Rust Client 迁移记

一曲冷凌霜 提交于 2020-03-24 08:43:10
3 月,跳不动了?>>> 作者介绍:Nick Cameron,PingCAP 研发工程师,Rust core team 成员,专注于分布式系统、数据库领域和 Rust 语言的进展。 最近我将一个中小型的 crate 从 futures 库的 0.1 迁移至了 0.3 版本。过程本身不是特别麻烦,但还是有些地方或是微妙棘手,或是没有很好的文档说明。这篇文章里,我会把迁移经验总结分享给大家。 我所迁移的 crate 是 TiKV 的 Rust Client 。该 crate 的规模约为 5500 行左右代码,通过 gRPC 与 TiKV 交互,采用异步接口实现。因此,对于 futures 库的使用颇为重度。 异步编程是 Rust 语言中影响广泛的一块领域,已有几年发展时间,其核心部分就是 futures 库。作为一个标准 Rust 库,futures 库为使用 futures 编程提供所需数据类型以及功能。虽然它是异步编程的关键,但并非你所需要的一切 - 你仍然需要可以推进事件循环 (event loop) 以及与操作系统交互的其他库。 futures 库在这几年中变化很大。最新的版本为 0.3(crates.io 发布的 futures 预览版)。然而,有许多早期代码是 futures 0.1 系列版本,且一直没有更新。这样的分裂事出有因 - 0.1 和 0.3 版本之间变化太大。0

最终,我们放弃了GO,迁移至Rust,特性使然

耗尽温柔 提交于 2020-03-24 08:36:33
3 月,跳不动了?>>> 云栖号: https://yqh.aliyun.com 第一手的上云资讯,不同行业精选的上云企业案例库,基于众多成功案例萃取而成的最佳实践,助力您上云决策! 在各个领域,Rust 都已经成为一流的语言。在 Discord,我们看到了 Rust 在客户端和服务端的成功。举例来说,我们在客户端使用它实现了 Go Live 的视频编码管道,在服务端,它则被用于 Elixir NIFs。最近,我们通过将服务的实现从 Go 切换到 Rust,极大地提升了该服务的性能。本文阐述了重新实现服务为何是有价值的、该过程是如何实现的以及由此带来的性能提升。 Read States 服务 Discord 是一家以产品为中心的公司,所以我们先介绍一下产品的背景信息。我们从 Go 切换到 Rust 的服务叫做“Read States”服务。它的唯一目的是跟踪用户阅读了哪些频道和信息。每当用户连接 Discord 的时候,每当消息发送的时候,每当消息被读取的时候,都会访问 Read States。简而言之,Read States 处于最关键的位置。我们希望能够保证 Discord 始终让人感觉快捷无比,所以必须要确保 Read States 是非常快速的。 在 Go 的实现中,Read States 无法支持产品的需求。在大多数情况下,它都是很快速的

How to delegate an async function with non-static parameter by a trait?

牧云@^-^@ 提交于 2020-03-23 07:55:08
问题 Like this code: use std::future::Future; use std::pin::Pin; trait A { fn handle<'a>(&'a self, data: &'a i32) -> Pin<Box<dyn 'a + Future<Output = ()>>>; } impl<'b, Fut> A for fn(&'b i32) -> Fut where Fut: 'b + Future<Output = ()>, { fn handle<'a>(&'a self, data: &'a i32) -> Pin<Box<dyn 'a + Future<Output = ()>>> { Box::pin(self(data)) } } how can I implement A for all async fn(&i32) ? 回答1: This code should works: use std::future::Future; use std::pin::Pin; trait A<'a> { fn handle(&'a self,

Result getting unexpected type argument

落花浮王杯 提交于 2020-03-23 07:48:38
问题 I am attempting to read values from a file in order to create a struct, and I'm getting a weird pair of errors. A super basic implementation of my code: extern crate itertools; use itertools::Itertools; use std::io::{self, prelude::*, BufReader}; use std::fs::{self, File}; // The struct I will unpack into struct BasicExample { a: String, b: String, c: String, d: String, } impl BasicExample { pub fn new(a: String, b: String, c: String, d: String} -> Self { BasicExample { a, b, c, d } } // I'm

Why does Serde not support Rc and Arc types by default?

▼魔方 西西 提交于 2020-03-22 08:26:33
问题 Please explain the Serde rc feature Opt into impls for Rc<T> and Arc<T> . Serializing and deserializing these types does not preserve identity and may result in multiple copies of the same data. Be sure that this is what you want before enabling this feature. Serializing a data structure containing reference-counted pointers will serialize a copy of the inner value of the pointer each time a pointer is referenced within the data structure. Serialization will not attempt to deduplicate these

Why does Serde not support Rc and Arc types by default?

左心房为你撑大大i 提交于 2020-03-22 08:26:27
问题 Please explain the Serde rc feature Opt into impls for Rc<T> and Arc<T> . Serializing and deserializing these types does not preserve identity and may result in multiple copies of the same data. Be sure that this is what you want before enabling this feature. Serializing a data structure containing reference-counted pointers will serialize a copy of the inner value of the pointer each time a pointer is referenced within the data structure. Serialization will not attempt to deduplicate these

Cargo, workspace and temporary local dependency

人走茶凉 提交于 2020-03-21 19:19:14
问题 I have two projects my_project and my_inner_project in one cargo workspace. They both depend on gfx (and gfx_core and gfx_device_gl). I've found a bug in gfx_device_core, so I've forked it, cloned, patched locally and want to test it before commit. Projects structure: -my_project --my_inner_project ---Cargo.toml --Cargo.toml -gfx --src ---core ----Cargo.toml #package gfx_core ---backend ----gl -----Cargo.toml #package gfx_device_gl ---render ----Cargo.toml #package gfx --Cargo.toml Now I want