Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Default not implemented for Mutex, RWLock, CondVar, Duration?

Tags:

rust

The Default trait can be #[derive(..)]d only if the contents of the deriving type also implement Default. This means the trait gets easier to use the more it is implemented. However, I notice some types from std are missing implementations, although they have perfectly valid defaults (sometimes depending on generic params).

  • Mutex<T> and RWLock<T> could implement by new(_) (where T: Default)
  • CondVar could simply implement by CondVar::new()
  • Duration could derive (to get a zero duration, which is a sensible default)

Is there a technical reason for those omissions?

like image 719
llogiq Avatar asked Nov 09 '22 17:11

llogiq


1 Answers

Some people have asked a similar question with Debug implementations, see “Missing Debug Implementations — #31869” which can also only be deriving under similar conditions as Default.

Unfortunately the corresponding PR “libcore: add Debug implementations to most missing types #32054” seems to suggest that some types were not Debug simply because no-one had written a Debug implementation for them. Some other types are kind of controversial about what the implementation should do and there is some fear about adding them in the standard library.

It reasonable to assume that at least some types are not Default for the same non-technical reasons.

like image 62
mcarton Avatar answered Jan 04 '23 03:01

mcarton