ownership

Is returning a reference ever a good idea?

£可爱£侵袭症+ 提交于 2019-12-02 04:35:47
We all know that returning a reference to a local variable is a bad idea . However, I'm wondering if it's ever really a good idea to a return a reference at all and if it's possible to determine some good rules about when or when not to do it. My problem with returning a reference is that the calling function needs to care about the lifetime of an object that shouldn't be its responsibility. As a contrived example: #include <vector> const int& foo() { std::vector<int> v = {1, 2, 3, 4, 5}; return v[0]; } int main(int argc, const char* argv[]) { const int& not_valid = foo(); return 0; } Here,

Take ownership of other users Drive-files using Google Apps Script

放肆的年华 提交于 2019-12-02 02:59:32
问题 Intro I'm tasked with creating a shared Google Drive folder-structure for my company. I want everything in (or parts of) it to be owned by the same user. To do this, I want to use Google Apps Script to force ownership of all files/folder within a folder, for a list of folders. Problem I cant get the ownership-transfer to work. Thoughts Google Apps Admins can force ownership from user X to user Y. But that wont do since I only want files places in the shared folder to have ownership

Memory leak in Qt5? How to get QMimeData deleted?

人盡茶涼 提交于 2019-12-01 18:05:37
I just provided an answer for this question and wanted to provide a working example when I noticed that newly created QMimeData instance returned by QListModel::mimeData() won't get deleted until the application gets terminated. So this is not a real memory leak since Qt handles all QMimeData instances on shutdown but you only have to drag&drop long enough and put the right content into your mime data to let the memory run full. Did I miss something? Is there a way to tell Qt to delete the QMimeData instances as soon as they are not needed any more? Please note: I know that every instance of

Memory leak in Qt5? How to get QMimeData deleted?

匆匆过客 提交于 2019-12-01 17:13:20
问题 I just provided an answer for this question and wanted to provide a working example when I noticed that newly created QMimeData instance returned by QListModel::mimeData() won't get deleted until the application gets terminated. So this is not a real memory leak since Qt handles all QMimeData instances on shutdown but you only have to drag&drop long enough and put the right content into your mime data to let the memory run full. Did I miss something? Is there a way to tell Qt to delete the

Why do I not need to explicitly lend a borrowed, mutable variable?

折月煮酒 提交于 2019-12-01 16:19:12
I've just written a small Rust program which calculates Fibonacci numbers and memoizes the calculation. It works, but I'm a little confused about why, especially the recursive call. (It also probably isn't idiomatic.) Here's the program: use std::collections::HashMap; fn main() { let n = 42; // hardcoded for simplicity let mut cache = HashMap::new(); let answer = fib(n, &mut cache); println!("fib of {} is {}", n, answer); } fn fib(n: i32, cache: &mut HashMap<i32,i32>) -> i32 { if cache.contains_key(&n) { return cache[&n]; } else { if n < 1 { panic!("must be >= 1") } let answer = if n == 1 { 0

cannot move out of borrowed content when unwrapping a member variable in a &mut self method

依然范特西╮ 提交于 2019-12-01 03:09:05
问题 I was trying to make a Disjoint-Set data structure in Rust. The relevant code is: pub struct Set<'a, T: 'a> { rank: u32, value: T, parent: Option<&'a mut Set<'a, T>>, } impl<'a, T> Set<'a, T> { pub fn find(&'a mut self) -> &'a mut Set<'a, T> { match self.parent { None => self, Some(mut p) => { self.parent = Some(p.find()); self.parent.unwrap() } } } } The errors I get are: error[E0507]: cannot move out of borrowed content --> src/main.rs:9:15 | 9 | match self.parent { | ^^^^ cannot move out

php script 403 forbidden error

只愿长相守 提交于 2019-11-30 19:04:36
I have a script giving me error 403 Forbidden error , it's just a copy of another script but the difference in this is that both use another mysql class to access database. My whole project is complete and this is last file so I don't want to do the whole work again for a single file. Server logs shows that client denied by server configuration: What should I look for? I have tried the following: Permissions are 644 New file with just simple echo gives 403 too Changed name of folder However, index.php works perfectly. Check the permissions and also ownership of the file. Generally, 403 means

How to use a file with a BufReader and still be able to write to it?

心不动则不痛 提交于 2019-11-30 15:10:58
I want to open a file and read its contents as a BufReader using lines() . I also want to be able to seek to the end of the file and write some new lines. Using let mut file lets me write to the file, but once I've given the file to the BufReader I can no longer write to it, as the main function no longer owns file : fn main() { let filename = "tt.txt"; // open a tt.txt file in the local directory let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(filename) .unwrap(); // now read the whole file to get the latest state let date_re = Regex::new(r"^(\d{4})-(\d{2})-(\d{2})"

Cannot infer an appropriate lifetime for a closure that returns a reference

牧云@^-^@ 提交于 2019-11-30 11:21:13
Considering the following code: fn foo<'a, T: 'a>(t: T) -> Box<Fn() -> &'a T + 'a> { Box::new(move || &t) } What I expect: The type T has lifetime 'a . The value t live as long as T . t moves to the closure, so the closure live as long as t The closure returns a reference to t which was moved to the closure. So the reference is valid as long as the closure exists. There is no lifetime problem, the code compiles. What actually happens: The code does not compile: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements --> src/lib.rs:2:22 | 2 | Box

php script 403 forbidden error

空扰寡人 提交于 2019-11-30 03:13:54
问题 I have a script giving me error 403 Forbidden error , it's just a copy of another script but the difference in this is that both use another mysql class to access database. My whole project is complete and this is last file so I don't want to do the whole work again for a single file. Server logs shows that client denied by server configuration: What should I look for? I have tried the following: Permissions are 644 New file with just simple echo gives 403 too Changed name of folder However,