lifetime-scoping

What does “Box<Fn() + Send + 'static>” mean in rust?

十年热恋 提交于 2019-12-09 09:45:14
问题 What does Box<Fn() + Send + 'static> mean in rust? I stumbled upon this syntax while reading advanced types chapter. Send is a trait but what does it mean to + a lifetime to a trait ( 'static in this case) in type parametrization ? Also what is Fn() ? 回答1: Let's decompose it one-by-one. Box Box<T> is a pointer to heap-allocated T . We use it here because trait objects can only exist behind pointers. Trait objects In Box<Fn() + Send + 'static> , Fn() + Send + 'static is a trait object type. In

Mediatr Scope problems

此生再无相见时 提交于 2019-12-07 15:15:22
问题 I am using Mediatr to handle messages from a queue. I can get a simple example to work. However I have run into problems when I try to inject an object into my handler public class MessageCommandHandler : IRequestHandler<MessageCommand, bool> { private IMyDependency myDependency; public MessageCommandHandler(IMyDependency myDependency) { this.myDependency = myDependency; } public Task<bool> Handle(MessageCommand request, CancellationToken cancellationToken) { return Task.FromResult(true); } }

DocumentDB client lifetime

谁说我不能喝 提交于 2019-12-05 07:09:13
To access DocumentDB/CosmosDB I'm using package Microsoft.Azure.DocumentDB.Core (v1.3.2). I have noticed when I create and initialise DocumentClient class: var documentClient = new DocumentClient(new Uri(endpointUrl), primaryKey); await documentClient.OpenAsync(); There is a number of requests fired to the endpoint to get information about indexes and other information. To be exact there are 9 HTTP requests going out on .OpenAsync() . This makes the creation and activation of the client a very costly operation in terms of performance - takes up to a second to get all the requests back home. So

What does “Box<Fn() + Send + 'static>” mean in rust?

本小妞迷上赌 提交于 2019-12-03 12:27:52
What does Box<Fn() + Send + 'static> mean in rust? I stumbled upon this syntax while reading advanced types chapter. Send is a trait but what does it mean to + a lifetime to a trait ( 'static in this case) in type parametrization ? Also what is Fn() ? Masaki Hara Let's decompose it one-by-one. Box Box<T> is a pointer to heap-allocated T . We use it here because trait objects can only exist behind pointers. Trait objects In Box<Fn() + Send + 'static> , Fn() + Send + 'static is a trait object type. In future, it will be written Box<dyn (Fn() + Send + 'static)> to avoid confusion. Inside dyn are

Implement IntoIterator for binary tree

旧街凉风 提交于 2019-12-02 19:37:57
问题 I am trying to build a binary tree and write an iterator to traverse values in the tree. When implementing the IntoIterator trait for my tree nodes I ran into a problem with lifetimes src\main.rs:43:6: 43:8 error: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates [E0207] src\main.rs:43 impl<'a, T: 'a> IntoIterator for Node<T> { I understand that I need to specify that NodeIterator will live as long as Node but I am unsure of how to express that use std

Implement IntoIterator for binary tree

喜你入骨 提交于 2019-12-02 12:24:10
I am trying to build a binary tree and write an iterator to traverse values in the tree. When implementing the IntoIterator trait for my tree nodes I ran into a problem with lifetimes src\main.rs:43:6: 43:8 error: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates [E0207] src\main.rs:43 impl<'a, T: 'a> IntoIterator for Node<T> { I understand that I need to specify that NodeIterator will live as long as Node but I am unsure of how to express that use std::cmp::PartialOrd; use std::boxed::Box; struct Node<T: PartialOrd> { value: T, left: Option<Box<Node<T>

Can multiple Autofac lifetime scopes be specified on a registration?

自古美人都是妖i 提交于 2019-11-30 07:27:10
I'm using the Autofac IoC container with the MVC4 add-on which provides the InstancePerHttpRequest lifetime scope. However within my project I have the web, web-api and background worker threads. In the following example I assume the InstancePerHttpRequest scope doesn't mean much when not originating from a web request. builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>() .InstancePerHttpRequest() I'm wondering if it is possible to do something like the following example and have the container choose the most appropriate lifetime scope? builder.RegisterType<DatabaseFactory>().As

Can multiple Autofac lifetime scopes be specified on a registration?

我只是一个虾纸丫 提交于 2019-11-29 09:01:00
问题 I'm using the Autofac IoC container with the MVC4 add-on which provides the InstancePerHttpRequest lifetime scope. However within my project I have the web, web-api and background worker threads. In the following example I assume the InstancePerHttpRequest scope doesn't mean much when not originating from a web request. builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>() .InstancePerHttpRequest() I'm wondering if it is possible to do something like the following example and have

How do I use static lifetimes with threads?

痴心易碎 提交于 2019-11-27 09:41:52
I'm currently struggling with lifetimes in Rust (1.0), especially when it comes to passing structs via channels. How would I get this simple example to compile: use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc; use std::thread::spawn; use std::io; use std::io::prelude::*; struct Message<'a> { text: &'a str, } fn main() { let (tx, rx): (Sender<Message>, Receiver<Message>) = mpsc::channel(); let _handle_receive = spawn(move || { for message in rx.iter() { println!("{}", message.text); } }); let stdin = io::stdin(); for line in stdin.lock().lines() { let message = Message { text:

How do I use static lifetimes with threads?

自闭症网瘾萝莉.ら 提交于 2019-11-26 14:49:22
问题 I'm currently struggling with lifetimes in Rust (1.0), especially when it comes to passing structs via channels. How would I get this simple example to compile: use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc; use std::thread::spawn; use std::io; use std::io::prelude::*; struct Message<'a> { text: &'a str, } fn main() { let (tx, rx): (Sender<Message>, Receiver<Message>) = mpsc::channel(); let _handle_receive = spawn(move || { for message in rx.iter() { println!("{}", message.text