-
Notifications
You must be signed in to change notification settings - Fork 20
Implement Read and Write for Arc<T> for types where &T implements Read and Write, *and* it is safe to do so. #504
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Note that if you're leading with "its inconsistent" and "it would be correct" that's generally not a problem statement. There are more things that could be done (valid programs/valid types) than can be stored on the atoms of the universe, most of them just aren't all that useful. ACPs that were exercises in feature-matrix-filling and motivated by consistency have been rejected before. So a stronger problem statement and motivation would be good. |
Better also point to rust-lang/rust#93044 (comment) showing why just avoiding I think if we further restrict the condition to
|
Ping! Could you please give us an example of a program that you are trying to write which these impls would help with? |
Sure! The general pattern I would like to follow is something like this, where a single object of an appropriate type is shared by a reader thread and a handle that you can use to writeonto the object. (I've simplified a lot of stuff; I hope it's still clear.)
I'd like to be able to use this with |
A good chunk of the types listed in the ACP ( |
Proposal
Problem statement
There's an inconsistency in the standard library at present:
Arc<File>
implementsRead
andWrite
, whereasArc<T>
for several otherRead+Write
types do not. In some cases (such as for[u8]
), it isn't correct to do so (see links below). But for several others, such as TcpStream and UnixStream, it would be correct.This proposal stands for the general proposition that, when
&T
implements Read and Write, and when it would be correct forArc<T>
to implement Read and Write, we should implement Read and Write onArc<T>
. It also stands for the specific proposal to implement Read and Write onArc<TcpStream>
andArc<UnixStream>
.(See also prior discussions at rust-lang/rust#53835 and rust-lang/rust#94744. I'm opening this ACP because I was asked to at #134190.)
(This is my first ACP; please let me know if I've done it wrong.)
Motivating examples or use cases
This is roughly the use case I have, though others exist:
(This is only one possible motivation. Prior discussion suggests that implementing Read and Write on
Arc<T>
, where it is correct to do so, would be useful in general for reasons of consistency.)Solution sketch
See PR at rust-lang/rust#134190.
At first glance, this could possibly be extended to these types, though I don't know if there is a reason to do so:
Arc<Stdout>
Arc<Stderr>
Arc<PipeWriter>
Arc<PipeReader>
Arc<ChildStdin>
Arc<ChildStdout>
Arc<Empty>
Arc<Sink>
Alternatives
There are numerous other workarounds that work for the motivating example, without changes to the library.
try_clone()
, at the cost of using an extra fd, and creating a fallible API.Read + Write
onNewType(Arc<T>)
, specifically for the cases where it is safe to do so. This requires a certain amount of boilerplate.Read + Write
onNewtype(Arc<Mutex<T>>)
for arbitrary T implementingRead + Write
. This requires a certain amount of boilerplate, and prevents simultaneous reads and writes.Within the standard library, there are other approaches for solving the motivating problem.
split_owned
, returning a separate ReadHalf and WriteHalf for each current duplex Read+Write type it provides. This might be desirable for other reasons (such as performance), but is orthogonal to this proposal.Read
andWrite
implementations for&T
whenever it is not correct for them to apply toArc<T>
. If it did so, it would then be safe to provide a blanketimpl<T> Read for Arc<T> where &T:Read .
&T
implements Read and it is correct to implement Read forArc<T>
. It could then provide a blanketimpl<T:ReadByRef> Read for Arc<T>
.Nonetheless, by analogy to the fact that
Arc<File>
currently implementsRead
andWrite
, I think that this approach would probably be the simplest and least controversial approach.Links and related work
Prior discussions of
impl {Read,Write} for Arc<T> where &T: {Read,Write}
:Read
,Write
andSeek
impls forArc<T>
where appropriate rust#94744Existing implementation of
impl Read,Write for Arc<File>
:Read
,Write
andSeek
impls forArc<File>
where appropriate rust#94748Read
,Write
andSeek
impls forArc<File>
where appropriate rust#94748 (comment) for a suggestion that this should be extended to other types.)Proposed implementation for
Arc<TcpStream>
,Arc<UnixStream>
Read
andWrite
onArc<TcpStream>
andArc<UnixStream>
. rust#134190The text was updated successfully, but these errors were encountered: