From 3d568896c29e3071a7582533418e02c6f32fa085 Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Sat, 26 Aug 2017 14:51:27 -0400 Subject: [PATCH] Updated with feedback from FCP. --- text/0000-non-exhaustive.md | 43 +++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/text/0000-non-exhaustive.md b/text/0000-non-exhaustive.md index acfd9067211..90b6f3a89fd 100644 --- a/text/0000-non-exhaustive.md +++ b/text/0000-non-exhaustive.md @@ -278,8 +278,8 @@ as well. # Detailed design An attribute `#[non_exhaustive]` is added to the language, which will (for now) -fail to compile if it's used on anything other than an enum, struct definition, -or enum variant. +fail to compile if it's used on anything other than an enum or struct +definition, or enum variant. ## Enums @@ -417,6 +417,10 @@ Then we the only valid way of matching will be: let Config { 0: width, 1: height, .. } = config; ``` +We can think of this as lowering the visibility of the constructor to +`pub(crate)` if it is marked as `pub`, then applying the standard structure +rules. + ## Unit structs Unit structs will work very similarly to tuple structs. Consider this struct: @@ -433,6 +437,41 @@ match it like: let Unit { .. } = unit; ``` +To users of this crate, this will act exactly as if the struct were defined as: + +``` +#[non_exhaustive] +pub struct Unit {} +``` + +## Functional record updates + +Functional record updates will operate exactly the same regardless of whether +structs are marked as non-exhaustive or not. For example, given this struct: + +``` +#[derive(Debug)] +#[non_exhaustive] +pub struct Config { + pub width: u16, + pub height: u16, + pub fullscreen: bool, +} +impl Default for Config { + fn default() -> Config { + Config { width: 640, height: 480, fullscreen: false } + } +} +``` + +The below code will print `Config { width: 1920, height: 1080, fullscreen: +false }` regardless of which crate is calling it: + +``` +let c = Config { width: 1920, height: 1080, ..Config::default() }; +println!("{:?}", c); +``` + ## Changes to rustdoc Right now, the only indicator that rustdoc gives for non-exhaustive enums and