I'm a bit confused about using TypedArena
. Currently I have the following code:
pub struct S<'a> {
arena: &'a mut TypedArena<Vec<u8>>
}
impl<'a> S<'a> {
pub fn new(arena: &'a mut TypedArena<Vec<u8>>) -> Self {
Self { arena: arena }
}
pub fn own(&mut self, data: Vec<u8>) -> &'a Vec<u8> {
self.arena.alloc(data)
}
}
The idea is to consume unique data
and return reference of it that has the same lifetime
as the arena ('a
). However this doesn't compile, with the following error message:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/bin/read_parquet.rs:16:16
|
16 | self.arena.alloc(data)
| ^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 15:54...
--> src/bin/read_parquet.rs:15:55
|
15 | pub fn own(&mut self, data: Vec<u8>) -> &'a Vec<u8> {
| _______________________________________________________^ starting here...
16 | | self.arena.alloc(data)
17 | | }
| |___^ ...ending here
note: ...so that reference does not outlive borrowed content
--> src/bin/read_parquet.rs:16:5
|
16 | self.arena.alloc(data)
| ^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the body at 15:54...
--> src/bin/read_parquet.rs:15:55
|
15 | pub fn own(&mut self, data: Vec<u8>) -> &'a Vec<u8> {
| _______________________________________________________^ starting here...
16 | | self.arena.alloc(data)
17 | | }
| |___^ ...ending here
note: ...so that reference does not outlive borrowed content
--> src/bin/read_parquet.rs:16:5
|
16 | self.arena.alloc(data)
| ^^^^^^^^^^^^^^^^^^^^^^
Can someone explain what's wrong in the code, and how can I achieve the purpose? Thanks.