-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #70016 - Dylan-DPC:rollup-5k7lxs3, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #69357 (Emit 1-based column numbers in debuginfo) - #69471 (Remove `sip::Hasher::short_write`.) - #69498 (Change "method" to "associated function") - #69967 (Remove a few `Rc`s from RegionInferenceCtxt) - #69987 (Add self to .mailmap) - #69991 (fix E0117 message out of sync) - #69993 (Add long error explanation for E0693) Failed merges: r? @ghost
- Loading branch information
Showing
95 changed files
with
383 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//! An immutable, owned value (except for interior mutability). | ||
//! | ||
//! The purpose of `Frozen` is to make a value immutable for the sake of defensive programming. For example, | ||
//! suppose we have the following: | ||
//! | ||
//! ```rust | ||
//! struct Bar { /* some data */ } | ||
//! | ||
//! struct Foo { | ||
//! /// Some computed data that should never change after construction. | ||
//! pub computed: Bar, | ||
//! | ||
//! /* some other fields */ | ||
//! } | ||
//! | ||
//! impl Bar { | ||
//! /// Mutate the `Bar`. | ||
//! pub fn mutate(&mut self) { } | ||
//! } | ||
//! ``` | ||
//! | ||
//! Now suppose we want to pass around a mutable `Foo` instance but, we want to make sure that | ||
//! `computed` does not change accidentally (e.g. somebody might accidentally call | ||
//! `foo.computed.mutate()`). This is what `Frozen` is for. We can do the following: | ||
//! | ||
//! ```rust | ||
//! use rustc_data_structures::frozen::Frozen; | ||
//! | ||
//! struct Foo { | ||
//! /// Some computed data that should never change after construction. | ||
//! pub computed: Frozen<Bar>, | ||
//! | ||
//! /* some other fields */ | ||
//! } | ||
//! ``` | ||
//! | ||
//! `Frozen` impls `Deref`, so we can ergonomically call methods on `Bar`, but it doesn't `impl | ||
//! DerefMut`. Now calling `foo.compute.mutate()` will result in a compile-time error stating that | ||
//! `mutate` requires a mutable reference but we don't have one. | ||
//! | ||
//! # Caveats | ||
//! | ||
//! - `Frozen` doesn't try to defend against interior mutability (e.g. `Frozen<RefCell<Bar>>`). | ||
//! - `Frozen` doesn't pin it's contents (e.g. one could still do `foo.computed = | ||
//! Frozen::freeze(new_bar)`). | ||
/// An owned immutable value. | ||
#[derive(Debug)] | ||
pub struct Frozen<T>(T); | ||
|
||
impl<T> Frozen<T> { | ||
pub fn freeze(val: T) -> Self { | ||
Frozen(val) | ||
} | ||
} | ||
|
||
impl<T> std::ops::Deref for Frozen<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &T { | ||
&self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.