rust/src/libstd
bors ab0d847277 auto merge of #13448 : alexcrichton/rust/rework-chan-return-values, r=brson
There are currently a number of return values from the std::comm methods, not
all of which are necessarily completely expressive:

 * `Sender::try_send(t: T) -> bool`
    This method currently doesn't transmit back the data `t` if the send fails
    due to the other end having disconnected. Additionally, this shares the name
    of the synchronous try_send method, but it differs in semantics in that it
    only has one failure case, not two (the buffer can never be full).

 * `SyncSender::try_send(t: T) -> TrySendResult<T>`
    This method accurately conveys all possible information, but it uses a
    custom type to the std::comm module with no convenience methods on it.
    Additionally, if you want to inspect the result you're forced to import
    something from `std::comm`.

 * `SyncSender::send_opt(t: T) -> Option<T>`
    This method uses Some(T) as an "error value" and None as a "success value",
    but almost all other uses of Option<T> have Some/None the other way

 * `Receiver::try_recv(t: T) -> TryRecvResult<T>`
    Similarly to the synchronous try_send, this custom return type is lacking in
    terms of usability (no convenience methods).

With this number of drawbacks in mind, I believed it was time to re-work the
return types of these methods. The new API for the comm module is:

    Sender::send(t: T) -> ()
    Sender::send_opt(t: T) -> Result<(), T>
    SyncSender::send(t: T) -> ()
    SyncSender::send_opt(t: T) -> Result<(), T>
    SyncSender::try_send(t: T) -> Result<(), TrySendError<T>>
    Receiver::recv() -> T
    Receiver::recv_opt() -> Result<T, ()>
    Receiver::try_recv() -> Result<T, TryRecvError>

The notable changes made are:

* Sender::try_send => Sender::send_opt. This renaming brings the semantics in
  line with the SyncSender::send_opt method. An asychronous send only has one
  failure case, unlike the synchronous try_send method which has two failure
  cases (full/disconnected).

* Sender::send_opt returns the data back to the caller if the send is guaranteed
  to fail. This method previously returned `bool`, but then it was unable to
  retrieve the data if the data was guaranteed to fail to send. There is still a
  race such that when `Ok(())` is returned the data could still fail to be
  received, but that's inherent to an asynchronous channel.

* Result is now the basis of all return values. This not only adds lots of
  convenience methods to all return values for free, but it also means that you
  can inspect the return values with no extra imports (Ok/Err are in the
  prelude). Additionally, it's now self documenting when something failed or not
  because the return value has "Err" in the name.

Things I'm a little uneasy about:

* The methods send_opt and recv_opt are not returning options, but rather
  results. I felt more strongly that Option was the wrong return type than the
  _opt prefix was wrong, and I coudn't think of a much better name for these
  methods. One possible way to think about them is to read the _opt suffix as
  "optionally".

* Result<T, ()> is often better expressed as Option<T>. This is only applicable
  to the recv_opt() method, but I thought it would be more consistent for
  everything to return Result rather than one method returning an Option.

Despite my two reasons to feel uneasy, I feel much better about the consistency
in return values at this point, and I think the only real open question is if
there's a better suffix for {send,recv}_opt.

Closes #11527
2014-04-12 12:21:58 -07:00
..
comm std: Make std::comm return types consistent 2014-04-10 21:41:19 -07:00
fmt auto merge of #13458 : huonw/rust/doc-signatures, r=alexcrichton 2014-04-11 12:01:44 -07:00
hash libtest: rename BenchHarness to Bencher 2014-04-11 17:31:13 +08:00
io auto merge of #13448 : alexcrichton/rust/rework-chan-return-values, r=brson 2014-04-12 12:21:58 -07:00
num libtest: rename BenchHarness to Bencher 2014-04-11 17:31:13 +08:00
path auto merge of #13395 : Ryman/rust/bytecontainer_impl_container, r=alexcrichton 2014-04-11 13:46:45 -07:00
rt auto merge of #13448 : alexcrichton/rust/rework-chan-return-values, r=brson 2014-04-12 12:21:58 -07:00
sync Add more type signatures to the docs; tweak a few of them. 2014-04-11 23:10:22 +10:00
unstable Add more type signatures to the docs; tweak a few of them. 2014-04-11 23:10:22 +10:00
any.rs libtest: rename BenchHarness to Bencher 2014-04-11 17:31:13 +08:00
ascii.rs Fix fallout of requiring uint indices 2014-04-02 15:56:31 -07:00
bool.rs std: Change assert_eq!() to use {} instead of {:?} 2014-02-28 23:01:54 -08:00
c_str.rs libtest: rename BenchHarness to Bencher 2014-04-11 17:31:13 +08:00
c_vec.rs Register new snapshots 2014-04-08 00:03:11 -07:00
cast.rs Remove std::cast::transmute_immut_unsafe 2014-03-19 16:15:22 +01:00
cell.rs Stop using transmute_mut in RefCell 2014-04-10 15:21:59 -07:00
char.rs libstd: Implement StrBuf, a new string buffer type like Vec, and 2014-04-10 22:10:10 +10:00
cleanup.rs Convert most code to new inner attribute syntax. 2014-03-28 17:12:21 -07:00
clone.rs Made the clone_from implementation for ~T reuse the T itself if 2014-03-18 16:29:57 -07:00
cmp.rs Remove use of block comments in src/libstd/cmp.rs 2014-04-06 16:21:36 +02:00
container.rs std: uniform modules titles for doc 2013-12-27 09:49:11 +01:00
default.rs libstd: Remove all support code related to @mut 2014-01-03 14:02:00 -08:00
from_str.rs std: uniform modules titles for doc 2013-12-27 09:49:11 +01:00
gc.rs std: Switch field privacy as necessary 2014-03-31 15:17:12 -07:00
intrinsics.rs rustc: remove ty_unboxed_vec. 2014-04-06 14:05:32 +03:00
iter.rs Register new snapshots 2014-04-08 00:03:11 -07:00
kinds.rs std: Switch field privacy as necessary 2014-03-31 15:17:12 -07:00
lib.rs libstd: Implement StrBuf, a new string buffer type like Vec, and 2014-04-10 22:10:10 +10:00
local_data.rs std,serialize: remove some internal uses of ~[]. 2014-04-10 15:21:58 -07:00
macros.rs std: use a match in assert_eq! to extend the lifetime of the args. 2014-04-09 09:57:49 +10:00
managed.rs std: remove the equals method from TotalEq. 2014-03-23 23:48:10 +11:00
mem.rs libtest: rename BenchHarness to Bencher 2014-04-11 17:31:13 +08:00
ops.rs libtest: rename BenchHarness to Bencher 2014-04-11 17:31:13 +08:00
option.rs std: Remove RefCell::set() 2014-04-03 20:28:59 -07:00
os.rs Fix fallout from std::libc separation 2014-04-04 09:31:44 -07:00
owned.rs std: remove the equals method from TotalEq. 2014-03-23 23:48:10 +11:00
prelude.rs libstd: Implement StrBuf, a new string buffer type like Vec, and 2014-04-10 22:10:10 +10:00
ptr.rs std: Add more docs for ptr mod 2014-04-08 00:03:11 -07:00
raw.rs std: Switch field privacy as necessary 2014-03-31 15:17:12 -07:00
rc.rs std: Switch field privacy as necessary 2014-03-31 15:17:12 -07:00
reference.rs std: remove the equals method from TotalEq. 2014-03-23 23:48:10 +11:00
reflect.rs rustc: remove ty_unboxed_vec. 2014-04-06 14:05:32 +03:00
repr.rs std,serialize: remove some internal uses of ~[]. 2014-04-10 15:21:58 -07:00
result.rs Rename from_iterator to from_iter for consistency. 2014-03-30 21:45:55 -07:00
rtdeps.rs Remove libc from std 2014-04-04 09:31:21 -07:00
slice.rs libtest: rename BenchHarness to Bencher 2014-04-11 17:31:13 +08:00
str.rs libtest: rename BenchHarness to Bencher 2014-04-11 17:31:13 +08:00
strbuf.rs libtest: rename BenchHarness to Bencher 2014-04-11 17:31:13 +08:00
task.rs Register new snapshots 2014-04-08 00:03:11 -07:00
to_str.rs Remove all ToStr impls, add Show impls 2014-02-23 20:51:56 -08:00
tuple.rs Convert most code to new inner attribute syntax. 2014-03-28 17:12:21 -07:00
ty.rs Fix an unnecessary use of cast::transmute 2014-04-05 20:38:35 +02:00
unicode.rs Convert most code to new inner attribute syntax. 2014-03-28 17:12:21 -07:00
unit.rs std: remove the equals method from TotalEq. 2014-03-23 23:48:10 +11:00
vec.rs libstd: Implement StrBuf, a new string buffer type like Vec, and 2014-04-10 22:10:10 +10:00