prokopyl / selfie Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
So in particular, your function signatures for referential
, with_referential
, and with_referential_mut
are all unsound.
The latter two can be fixed, look at the kinds of signatures that ouroboros or self_cell generate to see what approach can work soundly. On the other hand, the function referential
is entirely impossible to write soundly, as far as I’m aware, without resorting to macros or unsafe traits, in order to check for covariance so that the lifetime of <R as RefType<'s>>::Ref
is legal to change.
“But where did any lifetimes change?” – the change happens by the fact that the callbacks or return value in this API receives a reference to <R as RefType<'s>>::Ref
where 's
is taken from the lifetime of &'s self
. Since every borrow of self
in subsequent calls is a new borrow with a different lifetime, you just give inconsistently typed views of <R as RefType<'s>>::Ref
with different short-lived lifetimes, which will be unsound if 's
is not covariant in <R as RefType<'s>>::Ref
(with_referential_mut
is unsound regardless of variance).
For example, if you use <R as RefType<'s>>::Ref
= Cell<&'a …>
, then one call to with_referential
/with_referential_mut
allows placing something way-too-shortlived into that cell, and the next call can access it after free:
use selfie::{refs::RefType, Selfie};
use std::cell::Cell;
struct CellRefStr;
impl<'a> RefType<'a> for CellRefStr {
type Ref = Cell<&'a str>;
}
fn main() {
let x = Selfie::<_, CellRefStr>::new(Box::pin(()), |_| Cell::new(""));
let s = String::from("Hello World");
x.with_referential(|r| r.set(&s));
let r: &str = x.with_referential(|r| r.get());
println!("String exists: {r}");
drop(s);
println!("String used after free: {r}");
}
Of course, the above works just as well with with_referential_mut
.
For referential
, we need a Copy
type; but for instance &'a Cell<&'a …>
will work fine (with a little bit of leaking in order to construct it).
use selfie::{refs::RefType, Selfie};
use std::cell::Cell;
struct RefCellRefStr;
impl<'a> RefType<'a> for RefCellRefStr {
type Ref = &'a Cell<&'a str>;
}
fn main() {
let x = Selfie::<_, RefCellRefStr>::new(Box::pin(()), |_| Box::leak(Box::new(Cell::new(""))));
let s = String::from("Hello World");
x.referential().set(&s);
let r: &str = x.referential().get();
println!("String exists: {r}");
drop(s);
println!("String used after free: {r}");
}
Salut Adrien,
Thank you for selfie
.
This, I believe, is not an issue with selfie
, but rather my understanding of Pin
, or a difficulty with Rust types/lifetimes. Instead of having a selfie-based type only used for a local variable (and hence inferred), I'd like to use it as a part of a larger struct
(or enum
with data field(s)). Is that possible, and how?
I understand that the whole struct
/enum
could not be moved. Would the whole struct/enum
have to be used inside Pin
, or could it be an ordinary struct
(or enum
) but not a Copy
and with custom clone()
?
use selfie::{SelfieMut, refs::Ref, refs::RefType};
type Inner<'a> where Ref<&'a str>: for<'this> RefType<'this> = SelfieMut<'a, [u8; 8], Ref<&'a str>>;
// same error when I add this bound - probably redundant, based on https://github.com/rust-lang/rust/issues/70263 (which is not Pin-specific, however)
// type Inner<'a> where Ref<&'a str>: for<'this> RefType<'this> = SelfieMut<'a, [u8; 8], Ref<&'a str>>;
pub struct Outer<'a> {
inner: Inner<'a>,
}
which fails with: implementation of
RefType is not general enough
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.