3 ms·
Yep, it can be done with Pin and unsafe only, AFAIK. In theory, Rust can be extended to have relative references, but it's much simpler to use arrays and indexe
by drran 5y ago
Yep, it can be done with Pin and unsafe only, AFAIK. In theory, Rust can be extended to have relative references, but it's much simpler to use arrays and indexes and ranges instead.
With Pin:
use std::pin::Pin;
use std::marker::PhantomPinned;
#[derive(Debug)]
struct Input<'a>{
words: Vec<&'a str>,
phrase: Box<str>,
_marker: PhantomPinned,
}
fn test<'a>(s: String) -> Pin<Box<Input <'a>>> {
let bs = s.into_boxed_str();
let mut input = Box::pin( Input {
phrase: bs,
words: Vec::new(),
_marker: PhantomPinned,
});
unsafe {
let phrase: *const Box<str> = &input.as_ref().phrase;
let words = phrase.as_ref().unwrap().split_whitespace().collect();
input.as_mut().get_unchecked_mut().words = words;
}
input
}
fn main() {
let s="foo bar baz".to_string();
dbg!(test(s));
}