lifetime

Lifetime parameter problem in custom iterator over mutable references

寵の児 提交于 2020-08-10 20:00:31
问题 I'd like to implement a custom Iterator like below, but cannot solve reference problem. use itertools::Product; use std::ops::Range; struct Iter2DMut<'a, T: 'a> { data: &'a mut [T], shape: (usize, usize), idx_iter: Product<Range<usize>, Range<usize>>, } impl<'a, T: 'a> Iterator for Iter2DMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { if let Some((i, j)) = self.idx_iter.next() { Some(&mut self.data[i + self.shape.0 * j]) } else { None } } } and get the following

How can I get impl Trait to use the appropriate lifetime for a mutable reference to a value with another lifetime in it?

耗尽温柔 提交于 2020-07-19 04:07:49
问题 I have a struct with a lifetime: struct HasLifetime<'a>( /* ... */ ); There is there is an implementation of the trait Foo : impl<'a, 'b: 'a> Foo for &'a mut HasLifetime<'b> { } I want to implement the following function: fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut Lifetime<'b>) -> impl Foo { bar } This won't compile because the returned impl is only valid for 'a . However, specifying impl Foo + 'a results in: error[E0909]: hidden type for `impl Trait` captures lifetime that does not appear in

How can I get impl Trait to use the appropriate lifetime for a mutable reference to a value with another lifetime in it?

↘锁芯ラ 提交于 2020-07-19 04:04:00
问题 I have a struct with a lifetime: struct HasLifetime<'a>( /* ... */ ); There is there is an implementation of the trait Foo : impl<'a, 'b: 'a> Foo for &'a mut HasLifetime<'b> { } I want to implement the following function: fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut Lifetime<'b>) -> impl Foo { bar } This won't compile because the returned impl is only valid for 'a . However, specifying impl Foo + 'a results in: error[E0909]: hidden type for `impl Trait` captures lifetime that does not appear in

How can I get impl Trait to use the appropriate lifetime for a mutable reference to a value with another lifetime in it?

廉价感情. 提交于 2020-07-19 04:03:08
问题 I have a struct with a lifetime: struct HasLifetime<'a>( /* ... */ ); There is there is an implementation of the trait Foo : impl<'a, 'b: 'a> Foo for &'a mut HasLifetime<'b> { } I want to implement the following function: fn bar_to_foo<'a, 'b: 'a>(bar: &'a mut Lifetime<'b>) -> impl Foo { bar } This won't compile because the returned impl is only valid for 'a . However, specifying impl Foo + 'a results in: error[E0909]: hidden type for `impl Trait` captures lifetime that does not appear in

Is pointer arithmetic on allocated storage allowed since C++20?

女生的网名这么多〃 提交于 2020-07-04 11:59:49
问题 In the C++20 standard, it is said that array types are implicit lifetime type . Does it mean that an array to a non implicit lifetime type can be implicitly created? The implicit creation of such an array would not cause creation of the array's elements? Consider this case: //implicit creation of an array of std::string //but not the std::string elements: void * ptr = operator new(sizeof (std::string) * 10); //use launder to get a "pointer to object" (which object?) std::string * sptr = std:

Rust “expected type” error prints mismatched types that are exactly the same

断了今生、忘了曾经 提交于 2020-06-27 09:59:05
问题 With nightly rust: Playground struct Foo<T, F: Fn(&T, &T) -> T> { value: T, func: F } fn main() { let lambda = |&x, &y| x + y; let foo = Foo { value: 5 as i32, func: lambda }; } Error message: Compiling playground v0.0.1 (/playground) error[E0308]: mismatched types --> src/main.rs:8:15 | 8 | let foo = Foo { | ^^^ one type is more general than the other | = note: expected type `std::ops::FnOnce<(&i32, &i32)>` found type `std::ops::FnOnce<(&i32, &i32)>` Note that the expected type and found

Rust trait field lifetime

♀尐吖头ヾ 提交于 2020-06-01 04:48:26
问题 I think this is something obvious I'm missing, but here goes.. use std::io; pub trait Source<'a, T> { fn push(&self, t: T) -> io::Result<()>; fn link(&mut self, sink: &dyn Sink<'a, T>) -> io::Result<()>; } pub trait Sink<'a, T> { fn push(&self, t: T) -> io::Result<()>; fn link(&mut self, source: &dyn Source<T>) -> io::Result<()>; } pub struct SyncSource<'a, T> { sink: Option<&'a dyn Sink<'a, T>>, } impl<'a, T> SyncSource<'a, T> { pub fn new() -> SyncSource<'a, T> { SyncSource { sink: None, }

`cannot infer an appropriate lifetime for autoref due to conflicting requirements` but can't change anything due to trait definition constraints

元气小坏坏 提交于 2020-05-31 04:57:12
问题 I was implementing linked lists by following along too many linked lists. When trying to implement iter_mut() , I did it myself and made the following code: type Link<T> = Option<Box<Node<T>>>; pub struct List<T> { head: Link<T>, } struct Node<T> { elem: T, next: Link<T>, } impl<T> List<T> { pub fn iter_mut(&mut self) -> IterMut<T> { IterMut::<T>(&mut self.head) } } pub struct IterMut<'a, T>(&'a mut Link<T>); impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next<'b>(&'b mut

Sequential programatic copying in PyQt5

我是研究僧i 提交于 2020-05-16 19:12:56
问题 I have a PyQt5 application that shows a small list. It allows the user to copy list items. When the user copies a list item, it uses delayed rendering to place a reference to the item onto the clipboard. When the item is pasted from the clipboard, it attempts to toggle the selection and place the next item into the clipboard automatically. Delayed rendering works the first time. However, when I attempt to clear or re-use the clipboard, I get an internal Qt error which prints a message but