-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow borrowing a whole vec of pointers #3849
Comments
This would be quite complex. It's unlikely to happen purely automatically. However, we could perhaps add a method like Incidentally, @pcwalton has made the case to remove borrowing for function arguments altogether. I am not sure how I feel about it. On the one hand, I do prefer to err on the side of less magic. And borrowing and inference have the potential for surprising failures (though they seem to occur rarely in practice). On the other hand, it'll mean strictly more sigils than we have now (i.e., to convert an |
It couldn't happen automatically without special-case runtime support -- borrowing a ~ or @ into a & actually changes the value of the pointer (the &T skips past the refcount header). Borrowing the entire vector would have to mutate the contents of the whole vector. It's a shame that using a function like "borrow_all" would require memory allocation, though. |
Since this is 3 months old and there wasn't much enthusiasm, I'm closing. |
Could we change the representation of @ and ~ so that borrowing it doesn't require changing its value? (Subtract to reach the header, rather than add to reach the interior) |
Yes, I've thought about that from time to time. The only downside I can |
I don't see the connection between pointer representation (pointing at payload vs header) and payload alignment. |
So, imagine we had a one word header (I wish...) and a one character payload. In that event, today, we could build a tightly packed struct like But if the pointer is always at the payload, then we have to be able to find the header at a fixed negative offset, because sometimes we don't know the type of the payload and so we need to consult the header to figure it out. This means we'd have to always pad to the maximum supported alignment, regardless of the payload type. I'm not sure how big a problem this is in practice. It's just the only complication I could think of. |
the BoxChar children |
tree_borrows test: ensure we can actually read the variable
Most library functions can take
&
pointers, which is awesome. But library functions that take a vec of pointers have to decide how those pointers will be allocated, limiting their flexibility.The motivating example is
str::concat
. It is a pure function that takes&[~str]
and creates a new string with the concatenation of the input strings. It seems to me that it would be safe for it to take&[&str]
instead. (The slice is constant, and the callee is pure, so the pointers should remain valid.) But currently, that prevents a vec of~str
s from being passed.(For the special case of strings, there's the slight complication that
sys::size_of::<&static/str>()
is larger thansys::size_of::<~str>()
...)gives me errors like
The text was updated successfully, but these errors were encountered: