-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Make NonZero<char> possible #141001
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Make NonZero<char> possible #141001
Conversation
It’s not clear from the diff, but I guess since |
r? libs-api |
That's a good point I hadn't considered. I'll look into whether a feature gate is possible. |
Could you add a test so we don't accidentally regress this? |
Do you mean a test that just uses all the possible NonZero-able types as NonZeros? Meaning they are tested for being possible. Or is there another thing you meant? |
I did only mean something to check basic usage of the type, and was only thinking of |
372ef07
to
b9e0f3b
Compare
@tgross35 I added a simple test for |
Could give some examples of how this is going to be used in literal-escaper? |
The characters of a C string are checked for being non-null as a matter of correctness anyway, and it may be a good idea to preserve this in the type. I have a PR that did this (as a side quest) and the relevant file is here: https://github.com/rust-lang/rust/blob/30822ec0ec25723f36f9e73c42d91a83dc121388/compiler/rustc_lexer/src/unescape.rs /// Used for mixed utf8 string literals, i.e. those that allow both unicode
/// chars and high bytes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MixedUnit {
/// Used for ASCII chars (written directly or via `\x01`..`\x7f` escapes)
/// and Unicode chars (written directly or via `\u` escapes).
///
/// For example, if '¥' appears in a string it is represented here as
/// `MixedUnit::Char('¥')`, and it will be appended to the relevant byte
/// string as the two-byte UTF-8 sequence `[0xc2, 0xa5]`
Char(NonZero<char>),
/// Used for high bytes (`\x80`..`\xff`).
///
/// For example, if `\xa5` appears in a string it is represented here as
/// `MixedUnit::HighByte(0xa5)`, and it will be appended to the relevant
/// byte string as the single byte `0xa5`.
HighByte(NonZero<u8>),
}
impl From<NonZero<char>> for MixedUnit {
fn from(c: NonZero<char>) -> Self {
MixedUnit::Char(c)
}
}
impl From<NonZero<u8>> for MixedUnit {
fn from(byte: NonZero<u8>) -> Self {
if byte.get().is_ascii() {
MixedUnit::Char(NonZero::new(byte.get() as char).unwrap())
} else {
MixedUnit::HighByte(byte)
}
}
}
impl TryFrom<char> for MixedUnit {
type Error = EscapeError;
fn try_from(c: char) -> Result<Self, EscapeError> {
NonZero::new(c).map(MixedUnit::Char).ok_or(EscapeError::NulInCStr)
}
}
impl TryFrom<u8> for MixedUnit {
type Error = EscapeError;
fn try_from(byte: u8) -> Result<Self, EscapeError> {
NonZero::<u8>::new(byte).map(From::from).ok_or(EscapeError::NulInCStr)
}
} |
Seems reasonable. @rfcbot merge |
Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Make NonZero<char> possible I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
Rollup of 14 pull requests Successful merges: - #138062 (Enable Non-determinism of float operations in Miri and change std tests ) - #140560 (Allow `#![doc(test(attr(..)))]` everywhere) - #141001 (Make NonZero<char> possible) - #141295 (Stabilize `if let` guards (`feature(if_let_guard)`)) - #141435 (Add (back) `unsupported_calling_conventions` lint to reject more invalid calling conventions) - #141447 (Document representation of `Option<unsafe fn()>`) - #142008 (const-eval error: always say in which item the error occurred) - #142053 (Add new Tier-3 targets: `loongarch32-unknown-none*`) - #142065 (Stabilize `const_eq_ignore_ascii_case`) - #142116 (Fix bootstrap tracing imports) - #142126 (Treat normalizing consts like normalizing types in deeply normalize) - #142140 (compiler: Sort and doc ExternAbi variants) - #142148 (compiler: Treat ForceWarning as a Warning for diagnostic level) - #142154 (get rid of spurious cfg(bootstrap)) r? `@ghost` `@rustbot` modify labels: rollup
Make NonZero<char> possible I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
Rollup of 7 pull requests Successful merges: - #141001 (Make NonZero<char> possible) - #141700 (Atomic intrinsics : use const generic ordering, part 2) - #141993 (Use the in-tree `compiler-builtins` for the sysroot) - #142008 (const-eval error: always say in which item the error occurred) - #142053 (Add new Tier-3 targets: `loongarch32-unknown-none*`) - #142132 (`tests/ui`: A New Order [6/N]) - #142179 (store `target.min_global_align` as an `Align`) r? `@ghost` `@rustbot` modify labels: rollup
Make NonZero<char> possible I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
Rollup of 6 pull requests Successful merges: - #141001 (Make NonZero<char> possible) - #141700 (Atomic intrinsics : use const generic ordering, part 2) - #142008 (const-eval error: always say in which item the error occurred) - #142053 (Add new Tier-3 targets: `loongarch32-unknown-none*`) - #142132 (`tests/ui`: A New Order [6/N]) - #142179 (store `target.min_global_align` as an `Align`) r? `@ghost` `@rustbot` modify labels: rollup
Make NonZero<char> possible I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
Rollup of 11 pull requests Successful merges: - #140774 (Affirm `-Cforce-frame-pointers=off` does not override) - #141001 (Make NonZero<char> possible) - #141700 (Atomic intrinsics : use const generic ordering, part 2) - #142008 (const-eval error: always say in which item the error occurred) - #142053 (Add new Tier-3 targets: `loongarch32-unknown-none*`) - #142089 (Replace all uses of sysroot_candidates with get_or_default_sysroot) - #142108 (compiler: Add track_caller to AbiMapping::unwrap) - #142132 (`tests/ui`: A New Order [6/N]) - #142162 (UnsafePinned: update get() docs and signature to allow shared mutation) - #142171 (`tests/ui`: A New Order [7/N]) - #142179 (store `target.min_global_align` as an `Align`) r? `@ghost` `@rustbot` modify labels: rollup
Make NonZero<char> possible I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
💔 Test failed - checks-actions |
@bors retry "can't find crate for |
Cc @oli-obk (interacts with pattern-type-ification) |
Rollup of 5 pull requests Successful merges: - #140767 (Stabilize `sha512`, `sm3` and `sm4` for x86) - #141001 (Make NonZero<char> possible) - #141993 (Use the in-tree `compiler-builtins` for the sysroot) - #142208 (Always consider `const _` items as live for dead code analysis) - #142238 (stabilize nonnull_provenance) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #141001 - hkBst:nonzero-char, r=dtolnay Make NonZero<char> possible I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
Rollup of 5 pull requests Successful merges: - rust-lang/rust#140767 (Stabilize `sha512`, `sm3` and `sm4` for x86) - rust-lang/rust#141001 (Make NonZero<char> possible) - rust-lang/rust#141993 (Use the in-tree `compiler-builtins` for the sysroot) - rust-lang/rust#142208 (Always consider `const _` items as live for dead code analysis) - rust-lang/rust#142238 (stabilize nonnull_provenance) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 5 pull requests Successful merges: - rust-lang/rust#140767 (Stabilize `sha512`, `sm3` and `sm4` for x86) - rust-lang/rust#141001 (Make NonZero<char> possible) - rust-lang/rust#141993 (Use the in-tree `compiler-builtins` for the sysroot) - rust-lang/rust#142208 (Always consider `const _` items as live for dead code analysis) - rust-lang/rust#142238 (stabilize nonnull_provenance) r? `@ghost` `@rustbot` modify labels: rollup
Make NonZero<char> possible I'd like to use `NonZero<char>` for representing units of CStr in https://github.com/rust-lang/literal-escaper
I'd like to use
NonZero<char>
for representing units of CStr in https://github.com/rust-lang/literal-escaper