Skip to content

Commit e25ae61

Browse files
committed
docs(core): make more const_ptr doctests assert instead of printing
fixes rust-lang#124669
1 parent c4bd74c commit e25ae61

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

core/src/ptr/const_ptr.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl<T: ?Sized> *const T {
330330
///
331331
/// unsafe {
332332
/// if let Some(val_back) = ptr.as_ref() {
333-
/// println!("We got back the value: {val_back}!");
333+
/// assert_eq!(val_back, &10);
334334
/// }
335335
/// }
336336
/// ```
@@ -346,7 +346,7 @@ impl<T: ?Sized> *const T {
346346
///
347347
/// unsafe {
348348
/// let val_back = &*ptr;
349-
/// println!("We got back the value: {val_back}!");
349+
/// assert_eq!(val_back, &10);
350350
/// }
351351
/// ```
352352
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
@@ -393,7 +393,7 @@ impl<T: ?Sized> *const T {
393393
/// let ptr: *const u8 = &10u8 as *const u8;
394394
///
395395
/// unsafe {
396-
/// println!("We got back the value: {}!", ptr.as_ref_unchecked());
396+
/// assert_eq!(ptr.as_ref_unchecked(), &10);
397397
/// }
398398
/// ```
399399
// FIXME: mention it in the docs for `as_ref` and `as_uninit_ref` once stabilized.
@@ -439,7 +439,7 @@ impl<T: ?Sized> *const T {
439439
///
440440
/// unsafe {
441441
/// if let Some(val_back) = ptr.as_uninit_ref() {
442-
/// println!("We got back the value: {}!", val_back.assume_init());
442+
/// assert_eq!(val_back.assume_init(), 10);
443443
/// }
444444
/// }
445445
/// ```
@@ -501,8 +501,8 @@ impl<T: ?Sized> *const T {
501501
/// let ptr: *const u8 = s.as_ptr();
502502
///
503503
/// unsafe {
504-
/// println!("{}", *ptr.offset(1) as char);
505-
/// println!("{}", *ptr.offset(2) as char);
504+
/// assert_eq!(*ptr.offset(1) as char, '2');
505+
/// assert_eq!(*ptr.offset(2) as char, '3');
506506
/// }
507507
/// ```
508508
#[stable(feature = "rust1", since = "1.0.0")]
@@ -573,19 +573,21 @@ impl<T: ?Sized> *const T {
573573
/// # Examples
574574
///
575575
/// ```
576+
/// # use std::fmt::Write;
576577
/// // Iterate using a raw pointer in increments of two elements
577578
/// let data = [1u8, 2, 3, 4, 5];
578579
/// let mut ptr: *const u8 = data.as_ptr();
579580
/// let step = 2;
580581
/// let end_rounded_up = ptr.wrapping_offset(6);
581582
///
582-
/// // This loop prints "1, 3, 5, "
583+
/// let mut out = String::new();
583584
/// while ptr != end_rounded_up {
584585
/// unsafe {
585-
/// print!("{}, ", *ptr);
586+
/// write!(&mut out, "{}, ", *ptr).unwrap();
586587
/// }
587588
/// ptr = ptr.wrapping_offset(step);
588589
/// }
590+
/// assert_eq!(out.as_str(), "1, 3, 5, ");
589591
/// ```
590592
#[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
591593
#[must_use = "returns a new pointer rather than modifying its argument"]
@@ -988,8 +990,8 @@ impl<T: ?Sized> *const T {
988990
/// let ptr: *const u8 = s.as_ptr();
989991
///
990992
/// unsafe {
991-
/// println!("{}", *ptr.add(1) as char);
992-
/// println!("{}", *ptr.add(2) as char);
993+
/// assert_eq!(*ptr.add(1), b'2');
994+
/// assert_eq!(*ptr.add(2), b'3');
993995
/// }
994996
/// ```
995997
#[stable(feature = "pointer_methods", since = "1.26.0")]
@@ -1073,8 +1075,8 @@ impl<T: ?Sized> *const T {
10731075
///
10741076
/// unsafe {
10751077
/// let end: *const u8 = s.as_ptr().add(3);
1076-
/// println!("{}", *end.sub(1) as char);
1077-
/// println!("{}", *end.sub(2) as char);
1078+
/// assert_eq!(*end.sub(1), b'3');
1079+
/// assert_eq!(*end.sub(2), b'2');
10781080
/// }
10791081
/// ```
10801082
#[stable(feature = "pointer_methods", since = "1.26.0")]
@@ -1155,19 +1157,21 @@ impl<T: ?Sized> *const T {
11551157
/// # Examples
11561158
///
11571159
/// ```
1160+
/// # use std::fmt::Write;
11581161
/// // Iterate using a raw pointer in increments of two elements
11591162
/// let data = [1u8, 2, 3, 4, 5];
11601163
/// let mut ptr: *const u8 = data.as_ptr();
11611164
/// let step = 2;
11621165
/// let end_rounded_up = ptr.wrapping_add(6);
11631166
///
1164-
/// // This loop prints "1, 3, 5, "
1167+
/// let mut out = String::new();
11651168
/// while ptr != end_rounded_up {
11661169
/// unsafe {
1167-
/// print!("{}, ", *ptr);
1170+
/// write!(&mut out, "{}, ", *ptr).unwrap();
11681171
/// }
11691172
/// ptr = ptr.wrapping_add(step);
11701173
/// }
1174+
/// assert_eq!(out, "1, 3, 5, ");
11711175
/// ```
11721176
#[stable(feature = "pointer_methods", since = "1.26.0")]
11731177
#[must_use = "returns a new pointer rather than modifying its argument"]
@@ -1234,19 +1238,21 @@ impl<T: ?Sized> *const T {
12341238
/// # Examples
12351239
///
12361240
/// ```
1241+
/// # use std::fmt::Write;
12371242
/// // Iterate using a raw pointer in increments of two elements (backwards)
12381243
/// let data = [1u8, 2, 3, 4, 5];
12391244
/// let mut ptr: *const u8 = data.as_ptr();
12401245
/// let start_rounded_down = ptr.wrapping_sub(2);
12411246
/// ptr = ptr.wrapping_add(4);
12421247
/// let step = 2;
1243-
/// // This loop prints "5, 3, 1, "
1248+
/// let mut out = String::new();
12441249
/// while ptr != start_rounded_down {
12451250
/// unsafe {
1246-
/// print!("{}, ", *ptr);
1251+
/// write!(&mut out, "{}, ", *ptr).unwrap();
12471252
/// }
12481253
/// ptr = ptr.wrapping_sub(step);
12491254
/// }
1255+
/// assert_eq!(out, "5, 3, 1, ");
12501256
/// ```
12511257
#[stable(feature = "pointer_methods", since = "1.26.0")]
12521258
#[must_use = "returns a new pointer rather than modifying its argument"]

0 commit comments

Comments
 (0)