-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
format safety doc of Rc/Arc::from_raw/from_raw_in #154081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1430,29 +1430,32 @@ impl<T: ?Sized> Rc<T> { | |
| /// Constructs an `Rc<T>` from a raw pointer. | ||
| /// | ||
| /// The raw pointer must have been previously returned by a call to | ||
| /// [`Rc<U>::into_raw`][into_raw] with the following requirements: | ||
| /// [`Rc<U>::into_raw`][into_raw] or [`Rc<U>::into_raw_with_allocator`][into_raw_with_allocator]. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// * Creating a `Rc<T>` from a pointer other than one returned from | ||
| /// [`Rc<U>::into_raw`][into_raw] or [`Rc<U>::into_raw_with_allocator`][into_raw_with_allocator] | ||
| /// is undefined behavior. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing it is also undefined behavior if the pointer came from a call to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but there is another safety requirement in the safety section preventing this: the raw pointer must point to a block of memory allocated by the global allocator. |
||
| /// * If `U` is sized, it must have the same size and alignment as `T`. This | ||
| /// is trivially true if `U` is `T`. | ||
| /// * If `U` is unsized, its data pointer must have the same size and | ||
| /// alignment as `T`. This is trivially true if `Rc<U>` was constructed | ||
| /// through `Rc<T>` and then converted to `Rc<U>` through an [unsized | ||
| /// coercion]. | ||
| /// | ||
| /// Note that if `U` or `U`'s data pointer is not `T` but has the same size | ||
| /// and alignment, this is basically like transmuting references of | ||
| /// different types. See [`mem::transmute`][transmute] for more information | ||
| /// on what restrictions apply in this case. | ||
| /// | ||
| /// The raw pointer must point to a block of memory allocated by the global allocator | ||
| /// | ||
| /// The user of `from_raw` has to make sure a specific value of `T` is only | ||
| /// dropped once. | ||
| /// * Note that if `U` or `U`'s data pointer is not `T` but has the same size | ||
| /// and alignment, this is basically like transmuting references of | ||
| /// different types. See [`mem::transmute`][transmute] for more information | ||
| /// on what restrictions apply in this case. | ||
| /// * The raw pointer must point to a block of memory allocated by the global allocator | ||
| /// * The user of `from_raw` has to make sure a specific value of `T` is only | ||
| /// dropped once. | ||
| /// | ||
| /// This function is unsafe because improper use may lead to memory unsafety, | ||
| /// even if the returned `Rc<T>` is never accessed. | ||
| /// | ||
| /// [into_raw]: Rc::into_raw | ||
| /// [into_raw_with_allocator]: Rc::into_raw_with_allocator | ||
| /// [transmute]: core::mem::transmute | ||
| /// [unsized coercion]: https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions | ||
| /// | ||
|
|
@@ -1662,29 +1665,32 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> { | |
| /// Constructs an `Rc<T, A>` from a raw pointer in the provided allocator. | ||
| /// | ||
| /// The raw pointer must have been previously returned by a call to [`Rc<U, | ||
| /// A>::into_raw`][into_raw] with the following requirements: | ||
| /// A>::into_raw`][into_raw] or [`Rc<U, A>::into_raw_with_allocator`][into_raw_with_allocator]. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// * Creating a `Rc<T, A>` from a pointer other than one returned from | ||
| /// [`Rc<U, A>::into_raw`][into_raw] or [`Rc<U, A>::into_raw_with_allocator`][into_raw_with_allocator] | ||
| /// is undefined behavior. | ||
| /// * If `U` is sized, it must have the same size and alignment as `T`. This | ||
| /// is trivially true if `U` is `T`. | ||
| /// * If `U` is unsized, its data pointer must have the same size and | ||
| /// alignment as `T`. This is trivially true if `Rc<U>` was constructed | ||
| /// through `Rc<T>` and then converted to `Rc<U>` through an [unsized | ||
| /// alignment as `T`. This is trivially true if `Rc<U, A>` was constructed | ||
| /// through `Rc<T, A>` and then converted to `Rc<U, A>` through an [unsized | ||
| /// coercion]. | ||
| /// | ||
| /// Note that if `U` or `U`'s data pointer is not `T` but has the same size | ||
| /// and alignment, this is basically like transmuting references of | ||
| /// different types. See [`mem::transmute`][transmute] for more information | ||
| /// on what restrictions apply in this case. | ||
| /// | ||
| /// The raw pointer must point to a block of memory allocated by `alloc` | ||
| /// | ||
| /// The user of `from_raw` has to make sure a specific value of `T` is only | ||
| /// dropped once. | ||
| /// * Note that if `U` or `U`'s data pointer is not `T` but has the same size | ||
| /// and alignment, this is basically like transmuting references of | ||
| /// different types. See [`mem::transmute`][transmute] for more information | ||
| /// on what restrictions apply in this case. | ||
| /// * The raw pointer must point to a block of memory allocated by `alloc` | ||
| /// * The user of `from_raw` has to make sure a specific value of `T` is only | ||
| /// dropped once. | ||
| /// | ||
| /// This function is unsafe because improper use may lead to memory unsafety, | ||
| /// even if the returned `Rc<T>` is never accessed. | ||
| /// even if the returned `Rc<T, A>` is never accessed. | ||
| /// | ||
| /// [into_raw]: Rc::into_raw | ||
| /// [into_raw_with_allocator]: Rc::into_raw_with_allocator | ||
| /// [transmute]: core::mem::transmute | ||
| /// [unsized coercion]: https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions | ||
| /// | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This information is now duplicated in the safety section, but there it is phrased negatively instead. Should perhaps this positive version be moved into the safety section instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I referred to the format used in Thread::from_raw, which follows the same pattern.