|
1 | 1 | use core::clone::CloneToUninit;
|
| 2 | +use core::ffi::CStr; |
2 | 3 | use core::mem::MaybeUninit;
|
| 4 | +use core::ptr; |
3 | 5 |
|
4 | 6 | #[test]
|
5 | 7 | #[allow(suspicious_double_ref_op)]
|
@@ -80,3 +82,41 @@ fn test_clone_to_uninit_slice_drops_on_panic() {
|
80 | 82 | drop(a);
|
81 | 83 | assert_eq!(COUNTER.load(Relaxed), 0);
|
82 | 84 | }
|
| 85 | + |
| 86 | +#[test] |
| 87 | +fn test_clone_to_uninit_str() { |
| 88 | + let a = "hello"; |
| 89 | + |
| 90 | + let mut storage: MaybeUninit<[u8; 5]> = MaybeUninit::uninit(); |
| 91 | + unsafe { a.clone_to_uninit(storage.as_mut_ptr() as *mut [u8] as *mut str) }; |
| 92 | + assert_eq!(a.as_bytes(), unsafe { storage.assume_init() }.as_slice()); |
| 93 | + |
| 94 | + let mut b: Box<str> = "world".into(); |
| 95 | + assert_eq!(a.len(), b.len()); |
| 96 | + assert_ne!(a, &*b); |
| 97 | + unsafe { a.clone_to_uninit(ptr::from_mut::<str>(&mut b)) }; |
| 98 | + assert_eq!(a, &*b); |
| 99 | +} |
| 100 | + |
| 101 | +#[test] |
| 102 | +fn test_clone_to_uninit_cstr() { |
| 103 | + let a = c"hello"; |
| 104 | + |
| 105 | + let mut storage: MaybeUninit<[u8; 6]> = MaybeUninit::uninit(); |
| 106 | + unsafe { a.clone_to_uninit(storage.as_mut_ptr() as *mut [u8] as *mut CStr) }; |
| 107 | + assert_eq!(a.to_bytes_with_nul(), unsafe { storage.assume_init() }.as_slice()); |
| 108 | + |
| 109 | + let mut b: Box<CStr> = c"world".into(); |
| 110 | + assert_eq!(a.count_bytes(), b.count_bytes()); |
| 111 | + assert_ne!(a, &*b); |
| 112 | + unsafe { a.clone_to_uninit(ptr::from_mut::<CStr>(&mut b)) }; |
| 113 | + assert_eq!(a, &*b); |
| 114 | +} |
| 115 | + |
| 116 | +#[test] |
| 117 | +fn cstr_metadata_is_length_with_nul() { |
| 118 | + let s: &CStr = c"abcdef"; |
| 119 | + let p: *const CStr = ptr::from_ref(s); |
| 120 | + let bytes: *const [u8] = p as *const [u8]; |
| 121 | + assert_eq!(s.to_bytes_with_nul().len(), bytes.len()); |
| 122 | +} |
0 commit comments