Adds "not borrowed" std.socket.Socket implementation#11040
Conversation
51fef52 to
41a6393
Compare
1b56dc2 to
33ca83c
Compare
|
The usual solution for this is to set up strong ownership of the |
|
@CyberShadow I don't know how to do this without breaking the existing interface. I'm not opposed to breaking API, but the last time I tried to fix by changing |
33ca83c to
86ffa52
Compare
|
You can add overloads to say a constructor which takes a Flag instance. |
|
@rikkimax For some reason, I didn't like this idea last time, but I don't remember why. |
Not sure what you mean here. Here's an example of what I mean: struct SocketThing
{
typeof(scoped!Socket(socket_t.init, AddressFamily.init)) socket;
this(socket_t s)
{
this.socket = scoped!Socket(s, AddressFamily.INET);
}
~this()
{
this.socket.release(); // do not close
}
}This way we can borrow socket handles without duplicating the handle or modifying the standard library. |
|
@CyberShadow damn, yep! In fact we can just inherit Socket into our own class with own destructor calling release(). I still have a bad feeling because this solution requires knowledge of the internal structure of the Socket class, but I think the issue can be resolved without any Phobos changes. I don't think the |
Rationale
There is a case that is not covered by the
std.socket.Socket:When we get a ready-made socket_t from somewhere (from a 3rd-party library, for example), using it idiomatically in the D style, and after it we should just leave the socket "as is", because all other handling of this socket_t is performed by 3rd-party code.
Currently, if we want to do it this way, we have to duplicate the socket:
https://github.com/denizzzka/dpq2/blob/master/src/dpq2/connection.d#L227
and then
Socketwill close duplicate in desctructor without side-effects.Solution
Socketcode divided intoSocketand its baseSocketNotBorrowedclasses.SocketNotBorrowedjust not have destructor and not destroys passedsocket_t.In order not to break the documentation, it is need to separate documenting comments into a separate class. I made this in separate commit to convient reviewing.
Pre-review checklist
I think this case doesn't need additional testing because the solution to the problem is simply splitting
Socketcode into two parts, which are already covered by tests at the moment (I simply moved the constructors and destructor into a separate class)