简体   繁体   中英

Conflicting lifetimes when using predicate returning reference (implementing “split at mut” with predicate)

Is there a way to assign lifetimes here, such that function like this can be implemented even for predicates returning references?

fn group_by_into_slices_mut<'a, T, F, K>(data: &'a mut [T], key: F, res: &mut Vec<&'a mut [T]>)
where
    K: PartialEq,
    F: Fn(&T) -> K + 'static,
{
    let mut data = data;
    while !data.is_empty() {
        let j = find_j(&data, &key);
        let (s1, s2) = data.split_at_mut(j);
        res.push(s1);
        data = s2;
    }
}

fn find_j<'a, T, F, K>(data: &'a [T], key: &F) -> usize
where
    K: PartialEq,
    F: Fn(&T) -> K + 'static,
{
    let current_key = key(&data[0]);
    for i in 1..data.len() {
        if current_key != key(&data[i]) {
            return i;
        }
    }
    data.len()
}


struct Struct {
    key: String,
}

fn main() {
    let v = vec![Struct { key: "abc".to_string() }];
    let res = vec![];
    group_by_into_slices_mut(&mut v, |e| &e.key, &mut res);
}
  --> src/main.rs:37:42
   |
37 |     group_by_into_slices_mut(&mut v, |e| &e.key, &mut res);
   |                                          ^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 37:38...
  --> src/main.rs:37:38
   |
37 |     group_by_into_slices_mut(&mut v, |e| &e.key, &mut res);
   |                                      ^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:37:42
   |
37 |     group_by_into_slices_mut(&mut v, |e| &e.key, &mut res);
   |                                          ^^^^^^
note: but, the lifetime must be valid for the expression at 37:5...
  --> src/main.rs:37:5
   |
37 |     group_by_into_slices_mut(&mut v, |e| &e.key, &mut res);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so type `for<'a, 'r> fn(&'a mut [Struct], [closure@src/main.rs:37:38: 37:48], &'r mut std::vec::Vec<&'a mut [Struct]>) {group_by_into_slices_mut::<Struct, [closure@src/main.rs:37:38: 37:48], &std::string::String>}` of expression is valid during the expression
  --> src/main.rs:37:5
   |
37 |     group_by_into_slices_mut(&mut v, |e| &e.key, &mut res);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^

I'm not sure why this doesn't work. I was trying to add some lifetimes explicitly, including Higher-Rank Trait Bounds, but with no luck.

Playground:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1663e12bf67838dd0d1440f759a72b4e

Solution 1:

I don't think it's possible without keeping "abc" as an &'static str like your current trait bounds require. If you can change your Struct object to this:

struct Struct<'a> {
    key: &'a str
}

fn main() {
    let _ = Struct { key: "abc" }; //never convert "abc" to an owned type
}

...and pass your closure as |e| e.key |e| e.key instead of |e| &e.key |e| &e.key then your code compiles fine.

The function implementation doesn't change and the predicate still returns a reference but now that reference exists for 'static . A |e| &e.key |e| &e.key closure won't work because the returned reference is only allowed to exist for the body of the closure.

Solution 2:

Remove the 'static bounds on both F traits and change the closure to |e| e.key.clone() |e| e.key.clone() , but I'm guessing that is not what you're looking for because you wouldn't be returning a reference anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM