-
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? 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: master
Are you sure you want to change the base?
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. 🔔 |
I'd like to use
NonZero<char>
for representing units of CStr in https://github.com/rust-lang/literal-escaper