Skip to content

Commit 8008ba1

Browse files
committed
Cleanup
1 parent 65a3d91 commit 8008ba1

File tree

7 files changed

+79
-36
lines changed

7 files changed

+79
-36
lines changed

library/core/src/char/methods.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::*;
44
use crate::panic::const_panic;
55
use crate::slice;
66
use crate::str::from_utf8_unchecked_mut;
7-
//use crate::ub_checks::assert_unsafe_precondition;
7+
use crate::ub_checks::assert_unsafe_precondition;
88
use crate::unicode::printable::is_printable;
99
use crate::unicode::{self, conversions};
1010

@@ -1258,13 +1258,12 @@ impl char {
12581258
#[unstable(feature = "ascii_char", issue = "110998")]
12591259
#[inline]
12601260
pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
1261-
/*
12621261
assert_unsafe_precondition!(
12631262
check_library_ub,
1264-
"as_ascii_unchecked requires that the char is valid ASCII",
1265-
(it: &char = self) => it.is_ascii()
1263+
"as_ascii_unchecked requires that the char is valid ASCII \
1264+
(self:{it})",
1265+
(it: char = *self) => it.is_ascii()
12661266
);
1267-
*/
12681267

12691268
// SAFETY: the caller promised that this char is ASCII.
12701269
unsafe { ascii::Char::from_u8_unchecked(*self as u8) }

library/core/src/displaywrapper.rs

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ use core::fmt::{Display, Formatter, Result};
55
pub struct DisplayWrapper<T>(#[unstable(feature = "ub_checks", issue = "none")] pub T);
66

77
trait Displayable: Sized + Clone + Copy {
8-
const IS_POINTER: bool;
9-
const SIGNED: bool;
8+
const IS_POINTER: bool = false;
9+
const IS_INT: bool = false;
10+
const IS_UINT: bool = false;
11+
const IS_CHAR: bool = false;
12+
const IS_STR: bool = false;
13+
#[inline]
14+
fn as_char(self) -> char {
15+
unimplemented!()
16+
}
1017
#[inline]
1118
fn addr(self) -> usize {
1219
unimplemented!()
@@ -21,86 +28,128 @@ trait Displayable: Sized + Clone + Copy {
2128
}
2229
}
2330

31+
impl Displayable for char {
32+
const IS_CHAR: bool = true;
33+
#[inline]
34+
fn as_char(self) -> char {
35+
self
36+
}
37+
}
38+
2439
impl<T> Displayable for *const T {
2540
const IS_POINTER: bool = true;
26-
const SIGNED: bool = false;
2741
#[inline]
2842
fn addr(self) -> usize {
2943
self.addr()
3044
}
3145
}
3246
impl<T> Displayable for *mut T {
3347
const IS_POINTER: bool = true;
34-
const SIGNED: bool = false;
3548
#[inline]
3649
fn addr(self) -> usize {
3750
self.addr()
3851
}
3952
}
53+
4054
impl Displayable for u8 {
41-
const IS_POINTER: bool = false;
42-
const SIGNED: bool = false;
55+
const IS_UINT: bool = true;
4356
#[inline]
4457
fn as_u128(self) -> u128 {
4558
self as u128
4659
}
4760
}
4861
impl Displayable for u32 {
49-
const IS_POINTER: bool = false;
50-
const SIGNED: bool = false;
62+
const IS_UINT: bool = true;
5163
#[inline]
5264
fn as_u128(self) -> u128 {
5365
self as u128
5466
}
5567
}
5668
impl Displayable for u64 {
57-
const IS_POINTER: bool = false;
58-
const SIGNED: bool = false;
69+
const IS_UINT: bool = true;
5970
#[inline]
6071
fn as_u128(self) -> u128 {
6172
self as u128
6273
}
6374
}
6475
impl Displayable for usize {
65-
const IS_POINTER: bool = false;
66-
const SIGNED: bool = false;
76+
const IS_UINT: bool = true;
6777
#[inline]
6878
fn as_u128(self) -> u128 {
6979
self as u128
7080
}
7181
}
7282
impl Displayable for u128 {
73-
const IS_POINTER: bool = false;
74-
const SIGNED: bool = false;
83+
const IS_UINT: bool = true;
7584
#[inline]
7685
fn as_u128(self) -> u128 {
7786
self
7887
}
7988
}
8089

8190
impl Displayable for isize {
82-
const IS_POINTER: bool = false;
83-
const SIGNED: bool = true;
91+
const IS_INT: bool = true;
92+
#[inline]
93+
fn as_i128(self) -> i128 {
94+
self as i128
95+
}
96+
}
97+
impl Displayable for i8 {
98+
const IS_INT: bool = true;
99+
#[inline]
100+
fn as_i128(self) -> i128 {
101+
self as i128
102+
}
103+
}
104+
impl Displayable for i16 {
105+
const IS_INT: bool = true;
106+
#[inline]
107+
fn as_i128(self) -> i128 {
108+
self as i128
109+
}
110+
}
111+
impl Displayable for i32 {
112+
const IS_INT: bool = true;
113+
#[inline]
114+
fn as_i128(self) -> i128 {
115+
self as i128
116+
}
117+
}
118+
impl Displayable for i64 {
119+
const IS_INT: bool = true;
84120
#[inline]
85121
fn as_i128(self) -> i128 {
86122
self as i128
87123
}
88124
}
89125
impl Displayable for i128 {
90-
const IS_POINTER: bool = false;
91-
const SIGNED: bool = true;
126+
const IS_INT: bool = true;
92127
#[inline]
93128
fn as_i128(self) -> i128 {
94129
self
95130
}
96131
}
132+
97133
#[unstable(feature = "ub_checks", issue = "none")]
98134
impl<T: Displayable> Display for DisplayWrapper<T> {
99135
#[inline]
100136
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
101137
const HEX: [u8; 16] = *b"0123456789abcdef";
138+
assert!(T::IS_POINTER ^ T::IS_UINT ^ T::IS_INT ^ T::IS_CHAR);
139+
if T::IS_CHAR {
140+
let mut buf = [0u8; 4];
141+
let s = self.0.as_char().encode_utf8(&mut buf);
142+
return f.write_str(s);
143+
}
144+
/*
145+
if T::IS_STR {
146+
return f.write_str(self.0.as_str());
147+
}
148+
*/
149+
102150
let mut buf = [0u8; 42];
103151
let mut cur = buf.len();
152+
104153
if T::IS_POINTER {
105154
let mut n = self.0.addr();
106155
while n >= 16 {
@@ -118,7 +167,7 @@ impl<T: Displayable> Display for DisplayWrapper<T> {
118167
buf[cur] = b'0';
119168
} else {
120169
let mut is_negative = false;
121-
let mut n = if T::SIGNED {
170+
let mut n = if T::IS_INT {
122171
let signed = self.0.as_i128();
123172
is_negative = signed < 0;
124173
(!(signed as u128)).wrapping_add(1)

library/core/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,12 @@ pub mod alloc;
347347

348348
// note: does not need to be public
349349
mod bool;
350-
mod escape;
351-
mod tuple;
352-
mod unit;
353350
#[doc(hidden)]
354351
#[unstable(feature = "ub_checks", issue = "none")]
355352
pub mod displaywrapper;
353+
mod escape;
354+
mod tuple;
355+
mod unit;
356356

357357
#[stable(feature = "core_primitive", since = "1.43.0")]
358358
pub mod primitive;

library/core/src/num/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,11 @@ impl u8 {
506506
#[unstable(feature = "ascii_char", issue = "110998")]
507507
#[inline]
508508
pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
509-
/*
510509
assert_unsafe_precondition!(
511510
check_library_ub,
512511
"as_ascii_unchecked requires that the byte is valid ASCII",
513-
(it: &u8 = self) => it.is_ascii()
512+
(it: u8 = *self) => it.is_ascii()
514513
);
515-
*/
516514

517515
// SAFETY: the caller promised that this byte is ASCII.
518516
unsafe { ascii::Char::from_u8_unchecked(*self) }

library/core/src/num/nonzero.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ where
420420
ub_checks::assert_unsafe_precondition!(
421421
check_language_ub,
422422
"NonZero::new_unchecked requires the argument to be non-zero",
423-
// FIXME: Can't print n here because of how the check is written
424423
() => false,
425424
);
426425
intrinsics::unreachable()
@@ -462,7 +461,6 @@ where
462461
ub_checks::assert_unsafe_precondition!(
463462
check_library_ub,
464463
"NonZero::from_mut_unchecked requires the argument to dereference as non-zero",
465-
// FIXME: Can't print n here because of how the check is written
466464
() => false,
467465
);
468466
intrinsics::unreachable()

library/core/src/ptr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
15811581
unsafe {
15821582
ub_checks::assert_unsafe_precondition!(
15831583
check_language_ub,
1584-
"ptr::replace requires that the pointer argument is aligned and non-null\
1584+
"ptr::replace requires that the pointer argument is aligned and non-null \
15851585
(dst:{addr}, (align:{align}))",
15861586
(
15871587
addr: *const () = dst as *const (),

library/core/src/str/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use self::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher};
1717
use crate::char::{self, EscapeDebugExtArgs};
1818
use crate::ops::Range;
1919
use crate::slice::{self, SliceIndex};
20-
//use crate::ub_checks::assert_unsafe_precondition;
20+
use crate::ub_checks::assert_unsafe_precondition;
2121
use crate::{ascii, mem};
2222

2323
pub mod pattern;
@@ -2744,13 +2744,12 @@ impl str {
27442744
#[must_use]
27452745
#[inline]
27462746
pub const unsafe fn as_ascii_unchecked(&self) -> &[ascii::Char] {
2747-
/*
2747+
// FIXME: Add &str support to DisplayWrapper
27482748
assert_unsafe_precondition!(
27492749
check_library_ub,
27502750
"as_ascii_unchecked requires that the string is valid ASCII",
27512751
(it: &str = self) => it.is_ascii()
27522752
);
2753-
*/
27542753

27552754
// SAFETY: the caller promised that every byte of this string slice
27562755
// is ASCII.

0 commit comments

Comments
 (0)