Connor Horman
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# Deref Patterns, requirements and Syntax Tracking Document Discussion on irlo: https://internals.rust-lang.org/t/somewhat-random-idea-deref-patterns/13813 Discussion on Zulip: https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Deref.20patterns Older irlo discussion: https://internals.rust-lang.org/t/pre-pre-rfc-match-ergonomics-for-container-types-restricted-method-calls-in-patterns/13371 Also discussed on the box patterns issue: https://github.com/rust-lang/rust/issues/29641 ## Goal Be able to match through a `Deref` or `DerefMut` smart pointer ergonomically. ```rust let x: Option<Rc<bool>> = ...; match x { Some(deref true) => ..., Some(x) => ..., None => ..., } ``` ## Syntax options - No keyword or sigil at all - No keyword or sigil for literals (e.g. matching `String` with `"hello"`), keyword or sigil for non-literals (matching `String` with `x` and having `x` work as type `&str`) - Only allow literals, disallow non-literals - Sigil `*`: `*<pattern>` - Sigil `&`: `&<pattern>` - Overloads the existing `&` pattern - Keyword `box`: `box <pattern>` - Reuses and generalizes `box_pattern` syntax - This would likely be confusing for non-`Box` types - Keyword `deref`: `deref <pattern>` - This would require reserving a new keyword - Keyword would confict with the exact method this would work with. ## Constraints - this affects match exhaustiveness. If we don't restrict the `Deref` impls, then it is unsound to consider the following as exhaustive: ```rust let x: Option<SomePointer<bool>> = ...; match x { Some(deref true) => ..., Some(deref false) => ..., None => ..., } ``` - we can't generally commit to only `deref`ing once per subpattern, because e.g.: ```rust match x { Some(deref 0) => ..., Some(deref mut 1) => ..., Some(deref 2) => ..., } ``` - Note: this case could be implemented as a single `deref_mut`, then reborrowing. However, this would not work in a case like this: ```rust match &mut x { Some(deref v@0..3) => ..., // (1) Some(deref mut v@4..7) => ..., // (2) v@&None => ..., // (3) Some(deref v@8..11) => ..., // (4) } ``` - The above requires at least one deref_mut and one deref. Depending on the `Deref` impl, the shared borrow in pattern 3 may invalidate the mutable reference used in 1 and 2 (at the very least, for wrapper types like `ManuallyDrop`) - However, when `x` is immutable, it should be valid to only deref once. - if we allow side-effecting `Deref` impls, then people may start depending on the number and order of the `deref` calls, like they depended on drop order even if it was supposed to be unspecified. - Lang Team only wants "pure" `Deref` impls. But what about impure `Deref` that are idempotent (like `Lazy`/`SyncLazy`) - if we want the "no syntax" option, the type of `x` in `Some(x)` and `Some(x @ true)` could be different - that already happens with `&` and match ergonomics I think - some types can both be destructured and have a `Deref` impl - e.g. `Cow` and `AssertUnwindSafe` - outright ambiguous in the "no syntax" case: ```rust let cowcow: Cow<Cow<bool>> = ... match &cowcow { Cow::Owned(x) => {} // what's the type of `x`? } ``` - How would exhaustive matching handle mixing deref patterns with destructuring ```rust let unwind_safe: AssertUnwindSafe<bool> = ...; match &unwind_safe{ AssertUnwindSafe(true) => {}, deref false => {}, /* Would the above be considered exhasutive */ } ``` - Probably would be impossible for user-defined types; may be difficult for little advantage for standard library types (aside from adding `#[lang]` items to `Cow` and `AssertUnwindSafe`). - What about Cow when `T::Owned` is not `T` (`str`, `[T]`, custom `ToOwned` impl) - `DerefMove` still isn't a thing, yet we'd like to accomodate moving out of smart pointers in patterns in the future - so we need to be careful when things are owned to not default to taking `ref`s where in the future we would want to move out ```rust let x: Box<Option<bool>> = ... match x { Some(ref x) => {} // ok Some(ref mut x) => {} // ok Some(x) => {} // should error for now } ``` - mutability propagates backwards: the choice of `deref` vs `deref_mut` depends on subpatterns ```rust let x: Box<Option<bool>> = ... match x { Some(ref x) => {} Some(ref mut x) => {} } ``` - what happens if we mix `ref` and `ref mut` in a same pattern? Probably using `deref_mut` is ok. ## Possible solutions - Restrict to only a chosen set of Standard Library types - Current List: - Language-level References: `&T` and `&mut T` - Do we need to allow language-level references in deref patterns (absent generic deref patterns)? - Smart Pointers: `Box`, `Arc`, `Rc` - Precedent in Special Casing these in the standard library, with reciever types - Growable slice collections: `Vec`, `String`, `OsString`, `CString`, `PathBuf` - Vec and String also would be nice, since the deref targets (`[T]` and `str` resp.) - Mutex/RefCell guards: `Ref`, `RefMut`, `MutexGuard`, `RwLockReadGuard`, `RwLockWriteGuard` - Wrapper types: `IOSlice`, `IOSliceMut`, `ManuallyDrop`, `AssertUnwindSafe` (note: AssertUnwindSafe can be destructured) - ManuallyDrop in particular would be nice. - `Pin<P>` where `P` is a qualifying type - Likewise to `Box`, `Arc` and `Rc`, this has precedent in being special-cased in the language. - Could `Lazy` and `SyncLazy` qualify? - The implementations are not pure (can run arbitrary user-provided code), but are idempotent wrt. structure and address (and side effects) - Probably - Could `Cow` qualify? - `Deref` is idempotent wrt. structure and address - ~~`DerefMut` is not. The address stability question cannot be resolved. Additionally, it can invoke a user-provided `Clone`, which is a safe trait and may not produce a structurally equivalent value.~~ `Cow` does not implement `DefefMut` so this is not an issue - ~~Only an issue when mixing `Deref` and `DerefMut` accesses in patterns~~ - `Cow` can qualify for deref patterns, provided a `DerefMut` impl is not added. - Initial Project will persue this solution, providing the list of requirements for a standard library type to qualify, as well as propose an initial list - The standard library implementation would provide an unstable trait or attribute or otherwise (at it's option), applied to all qualifying types - Require a `_` pattern for the match to count as exhaustive ```rust let x: Option<SomePointer<bool>> = ...; match x { Some(true) => ..., Some(false) => ..., Some(_) => ..., None => ..., } ``` - people might rely on the order and number of `deref` calls, which would limit future changes - An unsafe `DerefPure` trait that restricts `deref`. - to make exhaustiveness sound, we need idempotency: successive calls must return "the same thing" - need at least structural stability between `deref{,_mut}` calls without an intervening access through `&mut` to the pointer or pointee, other than a `deref_mut` call. - ~~this disallows `DerefMut` for `Cow` since it must `clone` which may not preserve structural stability~~ Moot point, `Cow` is not `DerefMut` - unless we can restrict to structural clones - either with a new trait - or allowing only auto-derived `Clone` impls, akin to how only auto-derived `PartialEq` impls are allowed for constants in patterns - unless we disallow mixing `deref` and `deref_mut` on a same pointer - might also want address stability without an intervening move or access to the pointer through `&mut`. - would be useful for other optimizations too - could have stronger requirements like outright purity - would prevent `Lazy` - unsure what the benefits are - Some requirements are novel for unsafe traits. - restrict to `const` `deref` impls - Probably not enough to make exhaustiveness sound - could modify a `&mut` ref and thus not be idempotent - maybe enough if we don't allow `DerefMut`? - May be overly restrictive (e.g. a `Lazy` that reads a file) - Currently disqualifies all standard library smart pointers, due to not permitting raw pointer dereference.

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully