Michael Goulet
    • 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
    • Invite by email
      Invitee

      This note has no invitees

    • 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
    • Note Insights New
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
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
  • Invite by email
    Invitee

    This note has no invitees

  • 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Unsafe binder types ## Motivation Rust currently has no way of naming certain classes of existential lifetimes. For example, self-referential lifetimes: ```rust struct DataAndView { x: Box<[u8]>, element: & /* '??? */ u8, } ``` Often these lifetimes can be (unsafely) filled with `'static`, and with judicious usage of `transmute` and some manual safety guarantees, they can be wrapped with a sound API. However, these `'static` lifetimes can sometimes get in the way, since they often imply outlives-`'static` relationships with the data: ```rust struct DataAndView<T> { x: Box<[T]>, element: &'static T, //~^ ERROR the parameter type `T` may not live long enough } ``` This is further exacerbated by the presence of GATs and the `Self: 'a` bounds which are often required on them. While a coherent design for self-referential lifetimes relies on major design work and possible changes to the way we represent places, borrows, etc., designing a coherent but *unsafe* opt-out to the problem of "what do we put in this lifetime position other than `'static`" is a bit more tractable. ## Design ### The type: `unsafe<...> T` I'm proposing a new kind of type which I'll call an "unsafe binder". It's written like `unsafe<'a> &'a T`, and has two parts: * the binder `unsafe<'a, 'b, 'c>`, which introduces a set of lifetimes like a `for<'a>` binder. * the type, which is any type and which may reference the lifetimes introduced by the unsafe binder. An unsafe binder is a *distinct* class of type than the type it *wraps*. For example, `unsafe<'a> &'a T` is *not* a reference type, but a distinct type which *wraps* a reference. However, it inherits many of the properties of the type that it wraps (e.g. its `Sized`ness)[^yada]. [^yada]: I won't go into detail about the coherence semantics or anything like that. A lot of this design is still in the air. I envision it to be a bit like a `repr(transparent)` wrapper around the wrapped type, conceptually at least. The wrapped type of an unsafe binder may be any other type, including structs like `Foo<'a, 'b>`, slices, tuples, etc. The wrapped type may reference lifetimes from the unsafe binder *or* any other lifetime in scope, including `'static`, so this type is valid: `for<'a> fn(unsafe<'b> (&'a i32, &'b i32))`. ### Converting to and from `unsafe<...> T` Since I mentioned above that unsafe binder types are *distinct* types from the type that they wrap, you may be wondering how we intercovert between these types. I'm proposing the addition of two new operators which are represented with macros: `wrap_unsafe!(expr)` and `unwrap_unsafe!(expr)`. The former converts from, e.g., `&'_ T` to `unsafe<'a> &'a T`, and the latter converts in the other direction. These are distinct operators and not implemented with `as` casts since they... 1. Are place-preserving: `wrap_unsafe!` and `unwrap_unsafe!` operate on *places*, and will not perform moves unless the resulting expression is moved. This is useful when, for example, reading from an unsafe binder stored in a struct without moving out of the struct field. 2. Guide inference: They use the type of the argument and the expectation to guide inference. For example, given some `t: unsafe<'a> &'a i32`, the `unwrap_unsafe!(t)` operator can eagerly evaluate to `&'?0 i32`. This prevents unnecessary type ascription and can help prevent inference ambiguity errors. These operators are `unsafe` to use, since they are essentially a limited form of transmute between an unsafe binder and its wrapped type. Here's using unsafe binders in practice: ```rust struct SelfRef<T> { ptr: unsafe<'a> &'a T, data: Box<T>, } let s = SelfRef { ptr: unsafe { wrap_unsafe_binder!(&*data) }, data, }; let ptr = unsafe { unwrap_unsafe_binder!(s.ptr) }; ``` This may not be a very motivating example, so here's a [complete example which implements a `Map` combinator on async streams with async closures](https://gist.github.com/compiler-errors/26cdb5eb7da380102b8791308180858b). ## Why not just have some `'unsafe`? An unsafe lifetime introduces complications as it's not necessarily clear *where* the unsafety must be discharged. What constitutes an unsafe *usage*? Introducing a distinct typeclass which must be explicitly coerced to and from makes this very clear, since the unsafety must be discharged at the `wrap_unsafe`/`unwrap_unsafe` operators. The semantics of `'unsafe` embedded within normal types are not necessarily clear either. Do types which have `'unsafe` as one of their lifetime substitutions still impelement traits normally? How do they work with implied bounds? Etc. This is further confused by NLL, which treats lifetimes in MIR as "locations" in the MIR; this isn't clearly extensible to an unsafe lifetime. # TODO: - Copy/clone impl that is dependent on the inner type - ManuallyDrop or Copy inner type - Remove all the MIR jank from dropping

    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