| 1 |
#![warn(rust_2018_idioms)] |
| 2 |
|
| 3 |
use bytes::{Buf, BufMut, Bytes, BytesMut}; |
| 4 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 5 |
use std::sync::Arc; |
| 6 |
|
| 7 |
use std::panic::{self, AssertUnwindSafe}; |
| 8 |
|
| 9 |
const LONG: &[u8] = b"mary had a little lamb, little lamb, little lamb"; |
| 10 |
const SHORT: &[u8] = b"hello world"; |
| 11 |
|
| 12 |
fn is_sync<T: Sync>() {} |
| 13 |
fn is_send<T: Send>() {} |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn test_bounds() { |
| 17 |
is_sync::<Bytes>(); |
| 18 |
is_sync::<BytesMut>(); |
| 19 |
is_send::<Bytes>(); |
| 20 |
is_send::<BytesMut>(); |
| 21 |
} |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn test_layout() { |
| 25 |
use std::mem; |
| 26 |
|
| 27 |
assert_eq!( |
| 28 |
mem::size_of::<Bytes>(), |
| 29 |
mem::size_of::<usize>() * 4, |
| 30 |
"Bytes size should be 4 words", |
| 31 |
); |
| 32 |
assert_eq!( |
| 33 |
mem::size_of::<BytesMut>(), |
| 34 |
mem::size_of::<usize>() * 4, |
| 35 |
"BytesMut should be 4 words", |
| 36 |
); |
| 37 |
|
| 38 |
assert_eq!( |
| 39 |
mem::size_of::<Bytes>(), |
| 40 |
mem::size_of::<Option<Bytes>>(), |
| 41 |
"Bytes should be same size as Option<Bytes>", |
| 42 |
); |
| 43 |
|
| 44 |
assert_eq!( |
| 45 |
mem::size_of::<BytesMut>(), |
| 46 |
mem::size_of::<Option<BytesMut>>(), |
| 47 |
"BytesMut should be same size as Option<BytesMut>", |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn from_slice() { |
| 53 |
let a = Bytes::from(&b"abcdefgh"[..]); |
| 54 |
assert_eq!(a, b"abcdefgh"[..]); |
| 55 |
assert_eq!(a, &b"abcdefgh"[..]); |
| 56 |
assert_eq!(a, Vec::from(&b"abcdefgh"[..])); |
| 57 |
assert_eq!(b"abcdefgh"[..], a); |
| 58 |
assert_eq!(&b"abcdefgh"[..], a); |
| 59 |
assert_eq!(Vec::from(&b"abcdefgh"[..]), a); |
| 60 |
|
| 61 |
let a = BytesMut::from(&b"abcdefgh"[..]); |
| 62 |
assert_eq!(a, b"abcdefgh"[..]); |
| 63 |
assert_eq!(a, &b"abcdefgh"[..]); |
| 64 |
assert_eq!(a, Vec::from(&b"abcdefgh"[..])); |
| 65 |
assert_eq!(b"abcdefgh"[..], a); |
| 66 |
assert_eq!(&b"abcdefgh"[..], a); |
| 67 |
assert_eq!(Vec::from(&b"abcdefgh"[..]), a); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn fmt() { |
| 72 |
let a = format!("{:?}", Bytes::from(&b"abcdefg"[..])); |
| 73 |
let b = "b\"abcdefg\""; |
| 74 |
|
| 75 |
assert_eq!(a, b); |
| 76 |
|
| 77 |
let a = format!("{:?}", BytesMut::from(&b"abcdefg"[..])); |
| 78 |
assert_eq!(a, b); |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn fmt_write() { |
| 83 |
use std::fmt::Write; |
| 84 |
let s = String::from_iter((0..10).map(|_| "abcdefg")); |
| 85 |
|
| 86 |
let mut a = BytesMut::with_capacity(64); |
| 87 |
write!(a, "{}", &s[..64]).unwrap(); |
| 88 |
assert_eq!(a, s[..64].as_bytes()); |
| 89 |
|
| 90 |
let mut b = BytesMut::with_capacity(64); |
| 91 |
write!(b, "{}", &s[..32]).unwrap(); |
| 92 |
write!(b, "{}", &s[32..64]).unwrap(); |
| 93 |
assert_eq!(b, s[..64].as_bytes()); |
| 94 |
|
| 95 |
let mut c = BytesMut::with_capacity(64); |
| 96 |
write!(c, "{}", s).unwrap(); |
| 97 |
assert_eq!(c, s[..].as_bytes()); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn len() { |
| 102 |
let a = Bytes::from(&b"abcdefg"[..]); |
| 103 |
assert_eq!(a.len(), 7); |
| 104 |
|
| 105 |
let a = BytesMut::from(&b"abcdefg"[..]); |
| 106 |
assert_eq!(a.len(), 7); |
| 107 |
|
| 108 |
let a = Bytes::from(&b""[..]); |
| 109 |
assert!(a.is_empty()); |
| 110 |
|
| 111 |
let a = BytesMut::from(&b""[..]); |
| 112 |
assert!(a.is_empty()); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn index() { |
| 117 |
let a = Bytes::from(&b"hello world"[..]); |
| 118 |
assert_eq!(a[0..5], *b"hello"); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn slice() { |
| 123 |
let a = Bytes::from(&b"hello world"[..]); |
| 124 |
|
| 125 |
let b = a.slice(3..5); |
| 126 |
assert_eq!(b, b"lo"[..]); |
| 127 |
|
| 128 |
let b = a.slice(0..0); |
| 129 |
assert_eq!(b, b""[..]); |
| 130 |
|
| 131 |
let b = a.slice(3..3); |
| 132 |
assert_eq!(b, b""[..]); |
| 133 |
|
| 134 |
let b = a.slice(a.len()..a.len()); |
| 135 |
assert_eq!(b, b""[..]); |
| 136 |
|
| 137 |
let b = a.slice(..5); |
| 138 |
assert_eq!(b, b"hello"[..]); |
| 139 |
|
| 140 |
let b = a.slice(3..); |
| 141 |
assert_eq!(b, b"lo world"[..]); |
| 142 |
} |
| 143 |
|
| 144 |
#[test] |
| 145 |
#[should_panic] |
| 146 |
fn slice_oob_1() { |
| 147 |
let a = Bytes::from(&b"hello world"[..]); |
| 148 |
a.slice(5..44); |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
#[should_panic] |
| 153 |
fn slice_oob_2() { |
| 154 |
let a = Bytes::from(&b"hello world"[..]); |
| 155 |
a.slice(44..49); |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
#[should_panic] |
| 160 |
fn slice_start_greater_than_end() { |
| 161 |
let a = Bytes::from(&b"hello world"[..]); |
| 162 |
a.slice(5..3); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn split_off() { |
| 167 |
let mut hello = Bytes::from(&b"helloworld"[..]); |
| 168 |
let world = hello.split_off(5); |
| 169 |
|
| 170 |
assert_eq!(hello, &b"hello"[..]); |
| 171 |
assert_eq!(world, &b"world"[..]); |
| 172 |
|
| 173 |
let mut hello = BytesMut::from(&b"helloworld"[..]); |
| 174 |
let world = hello.split_off(5); |
| 175 |
|
| 176 |
assert_eq!(hello, &b"hello"[..]); |
| 177 |
assert_eq!(world, &b"world"[..]); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
#[should_panic] |
| 182 |
fn split_off_oob() { |
| 183 |
let mut hello = Bytes::from(&b"helloworld"[..]); |
| 184 |
let _ = hello.split_off(44); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
#[should_panic = "split_off out of bounds"] |
| 189 |
fn bytes_mut_split_off_oob() { |
| 190 |
let mut hello = BytesMut::from(&b"helloworld"[..]); |
| 191 |
let _ = hello.split_off(44); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn split_off_uninitialized() { |
| 196 |
let mut bytes = BytesMut::with_capacity(1024); |
| 197 |
let other = bytes.split_off(128); |
| 198 |
|
| 199 |
assert_eq!(bytes.len(), 0); |
| 200 |
assert_eq!(bytes.capacity(), 128); |
| 201 |
|
| 202 |
assert_eq!(other.len(), 0); |
| 203 |
assert_eq!(other.capacity(), 896); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn split_off_to_loop() { |
| 208 |
let s = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 209 |
|
| 210 |
for i in 0..(s.len() + 1) { |
| 211 |
{ |
| 212 |
let mut bytes = Bytes::from(&s[..]); |
| 213 |
let off = bytes.split_off(i); |
| 214 |
assert_eq!(i, bytes.len()); |
| 215 |
let mut sum = Vec::new(); |
| 216 |
sum.extend(bytes.iter()); |
| 217 |
sum.extend(off.iter()); |
| 218 |
assert_eq!(&s[..], &sum[..]); |
| 219 |
} |
| 220 |
{ |
| 221 |
let mut bytes = BytesMut::from(&s[..]); |
| 222 |
let off = bytes.split_off(i); |
| 223 |
assert_eq!(i, bytes.len()); |
| 224 |
let mut sum = Vec::new(); |
| 225 |
sum.extend(&bytes); |
| 226 |
sum.extend(&off); |
| 227 |
assert_eq!(&s[..], &sum[..]); |
| 228 |
} |
| 229 |
{ |
| 230 |
let mut bytes = Bytes::from(&s[..]); |
| 231 |
let off = bytes.split_to(i); |
| 232 |
assert_eq!(i, off.len()); |
| 233 |
let mut sum = Vec::new(); |
| 234 |
sum.extend(off.iter()); |
| 235 |
sum.extend(bytes.iter()); |
| 236 |
assert_eq!(&s[..], &sum[..]); |
| 237 |
} |
| 238 |
{ |
| 239 |
let mut bytes = BytesMut::from(&s[..]); |
| 240 |
let off = bytes.split_to(i); |
| 241 |
assert_eq!(i, off.len()); |
| 242 |
let mut sum = Vec::new(); |
| 243 |
sum.extend(&off); |
| 244 |
sum.extend(&bytes); |
| 245 |
assert_eq!(&s[..], &sum[..]); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn split_to_1() { |
| 252 |
// Static |
| 253 |
let mut a = Bytes::from_static(SHORT); |
| 254 |
let b = a.split_to(4); |
| 255 |
|
| 256 |
assert_eq!(SHORT[4..], a); |
| 257 |
assert_eq!(SHORT[..4], b); |
| 258 |
|
| 259 |
// Allocated |
| 260 |
let mut a = Bytes::copy_from_slice(LONG); |
| 261 |
let b = a.split_to(4); |
| 262 |
|
| 263 |
assert_eq!(LONG[4..], a); |
| 264 |
assert_eq!(LONG[..4], b); |
| 265 |
|
| 266 |
let mut a = Bytes::copy_from_slice(LONG); |
| 267 |
let b = a.split_to(30); |
| 268 |
|
| 269 |
assert_eq!(LONG[30..], a); |
| 270 |
assert_eq!(LONG[..30], b); |
| 271 |
} |
| 272 |
|
| 273 |
#[test] |
| 274 |
fn split_to_2() { |
| 275 |
let mut a = Bytes::from(LONG); |
| 276 |
assert_eq!(LONG, a); |
| 277 |
|
| 278 |
let b = a.split_to(1); |
| 279 |
|
| 280 |
assert_eq!(LONG[1..], a); |
| 281 |
drop(b); |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
#[should_panic] |
| 286 |
fn split_to_oob() { |
| 287 |
let mut hello = Bytes::from(&b"helloworld"[..]); |
| 288 |
let _ = hello.split_to(33); |
| 289 |
} |
| 290 |
|
| 291 |
#[test] |
| 292 |
#[should_panic] |
| 293 |
fn split_to_oob_mut() { |
| 294 |
let mut hello = BytesMut::from(&b"helloworld"[..]); |
| 295 |
let _ = hello.split_to(33); |
| 296 |
} |
| 297 |
|
| 298 |
#[test] |
| 299 |
#[should_panic] |
| 300 |
fn split_to_uninitialized() { |
| 301 |
let mut bytes = BytesMut::with_capacity(1024); |
| 302 |
let _other = bytes.split_to(128); |
| 303 |
} |
| 304 |
|
| 305 |
#[test] |
| 306 |
#[cfg_attr(not(panic = "unwind"), ignore)] |
| 307 |
fn split_off_to_at_gt_len() { |
| 308 |
fn make_bytes() -> Bytes { |
| 309 |
let mut bytes = BytesMut::with_capacity(100); |
| 310 |
bytes.put_slice(&[10, 20, 30, 40]); |
| 311 |
bytes.freeze() |
| 312 |
} |
| 313 |
|
| 314 |
use std::panic; |
| 315 |
|
| 316 |
let _ = make_bytes().split_to(4); |
| 317 |
let _ = make_bytes().split_off(4); |
| 318 |
|
| 319 |
assert!(panic::catch_unwind(move || { |
| 320 |
let _ = make_bytes().split_to(5); |
| 321 |
}) |
| 322 |
.is_err()); |
| 323 |
|
| 324 |
assert!(panic::catch_unwind(move || { |
| 325 |
let _ = make_bytes().split_off(5); |
| 326 |
}) |
| 327 |
.is_err()); |
| 328 |
} |
| 329 |
|
| 330 |
#[test] |
| 331 |
fn truncate() { |
| 332 |
let s = &b"helloworld"[..]; |
| 333 |
let mut hello = Bytes::from(s); |
| 334 |
hello.truncate(15); |
| 335 |
assert_eq!(hello, s); |
| 336 |
hello.truncate(10); |
| 337 |
assert_eq!(hello, s); |
| 338 |
hello.truncate(5); |
| 339 |
assert_eq!(hello, "hello"); |
| 340 |
} |
| 341 |
|
| 342 |
#[test] |
| 343 |
fn freeze_clone_shared() { |
| 344 |
let s = &b"abcdefgh"[..]; |
| 345 |
let b = BytesMut::from(s).split().freeze(); |
| 346 |
assert_eq!(b, s); |
| 347 |
let c = b.clone(); |
| 348 |
assert_eq!(c, s); |
| 349 |
} |
| 350 |
|
| 351 |
#[test] |
| 352 |
fn freeze_clone_unique() { |
| 353 |
let s = &b"abcdefgh"[..]; |
| 354 |
let b = BytesMut::from(s).freeze(); |
| 355 |
assert_eq!(b, s); |
| 356 |
let c = b.clone(); |
| 357 |
assert_eq!(c, s); |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn freeze_after_advance() { |
| 362 |
let s = &b"abcdefgh"[..]; |
| 363 |
let mut b = BytesMut::from(s); |
| 364 |
b.advance(1); |
| 365 |
assert_eq!(b, s[1..]); |
| 366 |
let b = b.freeze(); |
| 367 |
// Verify fix for #352. Previously, freeze would ignore the start offset |
| 368 |
// for BytesMuts in Vec mode. |
| 369 |
assert_eq!(b, s[1..]); |
| 370 |
} |
| 371 |
|
| 372 |
#[test] |
| 373 |
fn freeze_after_advance_arc() { |
| 374 |
let s = &b"abcdefgh"[..]; |
| 375 |
let mut b = BytesMut::from(s); |
| 376 |
// Make b Arc |
| 377 |
let _ = b.split_to(0); |
| 378 |
b.advance(1); |
| 379 |
assert_eq!(b, s[1..]); |
| 380 |
let b = b.freeze(); |
| 381 |
assert_eq!(b, s[1..]); |
| 382 |
} |
| 383 |
|
| 384 |
#[test] |
| 385 |
fn freeze_after_split_to() { |
| 386 |
let s = &b"abcdefgh"[..]; |
| 387 |
let mut b = BytesMut::from(s); |
| 388 |
let _ = b.split_to(1); |
| 389 |
assert_eq!(b, s[1..]); |
| 390 |
let b = b.freeze(); |
| 391 |
assert_eq!(b, s[1..]); |
| 392 |
} |
| 393 |
|
| 394 |
#[test] |
| 395 |
fn freeze_after_truncate() { |
| 396 |
let s = &b"abcdefgh"[..]; |
| 397 |
let mut b = BytesMut::from(s); |
| 398 |
b.truncate(7); |
| 399 |
assert_eq!(b, s[..7]); |
| 400 |
let b = b.freeze(); |
| 401 |
assert_eq!(b, s[..7]); |
| 402 |
} |
| 403 |
|
| 404 |
#[test] |
| 405 |
fn freeze_after_truncate_arc() { |
| 406 |
let s = &b"abcdefgh"[..]; |
| 407 |
let mut b = BytesMut::from(s); |
| 408 |
// Make b Arc |
| 409 |
let _ = b.split_to(0); |
| 410 |
b.truncate(7); |
| 411 |
assert_eq!(b, s[..7]); |
| 412 |
let b = b.freeze(); |
| 413 |
assert_eq!(b, s[..7]); |
| 414 |
} |
| 415 |
|
| 416 |
#[test] |
| 417 |
fn freeze_after_split_off() { |
| 418 |
let s = &b"abcdefgh"[..]; |
| 419 |
let mut b = BytesMut::from(s); |
| 420 |
let _ = b.split_off(7); |
| 421 |
assert_eq!(b, s[..7]); |
| 422 |
let b = b.freeze(); |
| 423 |
assert_eq!(b, s[..7]); |
| 424 |
} |
| 425 |
|
| 426 |
#[test] |
| 427 |
fn fns_defined_for_bytes_mut() { |
| 428 |
let mut bytes = BytesMut::from(&b"hello world"[..]); |
| 429 |
|
| 430 |
let _ = bytes.as_ptr(); |
| 431 |
let _ = bytes.as_mut_ptr(); |
| 432 |
|
| 433 |
// Iterator |
| 434 |
let v: Vec<u8> = bytes.as_ref().iter().cloned().collect(); |
| 435 |
assert_eq!(&v[..], bytes); |
| 436 |
} |
| 437 |
|
| 438 |
#[test] |
| 439 |
fn reserve_convert() { |
| 440 |
// Vec -> Vec |
| 441 |
let mut bytes = BytesMut::from(LONG); |
| 442 |
bytes.reserve(64); |
| 443 |
assert_eq!(bytes.capacity(), LONG.len() + 64); |
| 444 |
|
| 445 |
// Arc -> Vec |
| 446 |
let mut bytes = BytesMut::from(LONG); |
| 447 |
let a = bytes.split_to(30); |
| 448 |
|
| 449 |
bytes.reserve(128); |
| 450 |
assert!(bytes.capacity() >= bytes.len() + 128); |
| 451 |
|
| 452 |
drop(a); |
| 453 |
} |
| 454 |
|
| 455 |
#[test] |
| 456 |
fn reserve_growth() { |
| 457 |
let mut bytes = BytesMut::with_capacity(64); |
| 458 |
bytes.put("hello world".as_bytes()); |
| 459 |
let _ = bytes.split(); |
| 460 |
|
| 461 |
bytes.reserve(65); |
| 462 |
assert_eq!(bytes.capacity(), 117); |
| 463 |
} |
| 464 |
|
| 465 |
#[test] |
| 466 |
fn reserve_allocates_at_least_original_capacity() { |
| 467 |
let mut bytes = BytesMut::with_capacity(1024); |
| 468 |
|
| 469 |
for i in 0..1020 { |
| 470 |
bytes.put_u8(i as u8); |
| 471 |
} |
| 472 |
|
| 473 |
let _other = bytes.split(); |
| 474 |
|
| 475 |
bytes.reserve(16); |
| 476 |
assert_eq!(bytes.capacity(), 1024); |
| 477 |
} |
| 478 |
|
| 479 |
#[test] |
| 480 |
#[cfg_attr(miri, ignore)] // Miri is too slow |
| 481 |
fn reserve_max_original_capacity_value() { |
| 482 |
const SIZE: usize = 128 * 1024; |
| 483 |
|
| 484 |
let mut bytes = BytesMut::with_capacity(SIZE); |
| 485 |
|
| 486 |
for _ in 0..SIZE { |
| 487 |
bytes.put_u8(0u8); |
| 488 |
} |
| 489 |
|
| 490 |
let _other = bytes.split(); |
| 491 |
|
| 492 |
bytes.reserve(16); |
| 493 |
assert_eq!(bytes.capacity(), 64 * 1024); |
| 494 |
} |
| 495 |
|
| 496 |
#[test] |
| 497 |
fn reserve_vec_recycling() { |
| 498 |
let mut bytes = BytesMut::with_capacity(16); |
| 499 |
assert_eq!(bytes.capacity(), 16); |
| 500 |
let addr = bytes.as_ptr() as usize; |
| 501 |
bytes.put("0123456789012345".as_bytes()); |
| 502 |
assert_eq!(bytes.as_ptr() as usize, addr); |
| 503 |
bytes.advance(10); |
| 504 |
assert_eq!(bytes.capacity(), 6); |
| 505 |
bytes.reserve(8); |
| 506 |
assert_eq!(bytes.capacity(), 16); |
| 507 |
assert_eq!(bytes.as_ptr() as usize, addr); |
| 508 |
} |
| 509 |
|
| 510 |
#[test] |
| 511 |
fn reserve_in_arc_unique_does_not_overallocate() { |
| 512 |
let mut bytes = BytesMut::with_capacity(1000); |
| 513 |
let _ = bytes.split(); |
| 514 |
|
| 515 |
// now bytes is Arc and refcount == 1 |
| 516 |
|
| 517 |
assert_eq!(1000, bytes.capacity()); |
| 518 |
bytes.reserve(2001); |
| 519 |
assert_eq!(2001, bytes.capacity()); |
| 520 |
} |
| 521 |
|
| 522 |
#[test] |
| 523 |
fn reserve_in_arc_unique_doubles() { |
| 524 |
let mut bytes = BytesMut::with_capacity(1000); |
| 525 |
let _ = bytes.split(); |
| 526 |
|
| 527 |
// now bytes is Arc and refcount == 1 |
| 528 |
|
| 529 |
assert_eq!(1000, bytes.capacity()); |
| 530 |
bytes.reserve(1001); |
| 531 |
assert_eq!(2000, bytes.capacity()); |
| 532 |
} |
| 533 |
|
| 534 |
#[test] |
| 535 |
fn reserve_in_arc_unique_does_not_overallocate_after_split() { |
| 536 |
let mut bytes = BytesMut::from(LONG); |
| 537 |
let orig_capacity = bytes.capacity(); |
| 538 |
drop(bytes.split_off(LONG.len() / 2)); |
| 539 |
|
| 540 |
// now bytes is Arc and refcount == 1 |
| 541 |
|
| 542 |
let new_capacity = bytes.capacity(); |
| 543 |
bytes.reserve(orig_capacity - new_capacity); |
| 544 |
assert_eq!(bytes.capacity(), orig_capacity); |
| 545 |
} |
| 546 |
|
| 547 |
#[test] |
| 548 |
fn reserve_in_arc_unique_does_not_overallocate_after_multiple_splits() { |
| 549 |
let mut bytes = BytesMut::from(LONG); |
| 550 |
let orig_capacity = bytes.capacity(); |
| 551 |
for _ in 0..10 { |
| 552 |
drop(bytes.split_off(LONG.len() / 2)); |
| 553 |
|
| 554 |
// now bytes is Arc and refcount == 1 |
| 555 |
|
| 556 |
let new_capacity = bytes.capacity(); |
| 557 |
bytes.reserve(orig_capacity - new_capacity); |
| 558 |
} |
| 559 |
assert_eq!(bytes.capacity(), orig_capacity); |
| 560 |
} |
| 561 |
|
| 562 |
#[test] |
| 563 |
fn reserve_in_arc_nonunique_does_not_overallocate() { |
| 564 |
let mut bytes = BytesMut::with_capacity(1000); |
| 565 |
let _copy = bytes.split(); |
| 566 |
|
| 567 |
// now bytes is Arc and refcount == 2 |
| 568 |
|
| 569 |
assert_eq!(1000, bytes.capacity()); |
| 570 |
bytes.reserve(2001); |
| 571 |
assert_eq!(2001, bytes.capacity()); |
| 572 |
} |
| 573 |
|
| 574 |
/// This function tests `BytesMut::reserve_inner`, where `BytesMut` holds |
| 575 |
/// a unique reference to the shared vector and decide to reuse it |
| 576 |
/// by reallocating the `Vec`. |
| 577 |
#[test] |
| 578 |
fn reserve_shared_reuse() { |
| 579 |
let mut bytes = BytesMut::with_capacity(1000); |
| 580 |
bytes.put_slice(b"Hello, World!"); |
| 581 |
drop(bytes.split()); |
| 582 |
|
| 583 |
bytes.put_slice(b"!123ex123,sadchELLO,_wORLD!"); |
| 584 |
// Use split_off so that v.capacity() - self.cap != off |
| 585 |
drop(bytes.split_off(9)); |
| 586 |
assert_eq!(&*bytes, b"!123ex123"); |
| 587 |
|
| 588 |
bytes.reserve(2000); |
| 589 |
assert_eq!(&*bytes, b"!123ex123"); |
| 590 |
assert_eq!(bytes.capacity(), 2009); |
| 591 |
} |
| 592 |
|
| 593 |
#[test] |
| 594 |
fn extend_mut() { |
| 595 |
let mut bytes = BytesMut::with_capacity(0); |
| 596 |
bytes.extend(LONG); |
| 597 |
assert_eq!(*bytes, LONG[..]); |
| 598 |
} |
| 599 |
|
| 600 |
#[test] |
| 601 |
fn extend_from_slice_mut() { |
| 602 |
for &i in &[3, 34] { |
| 603 |
let mut bytes = BytesMut::new(); |
| 604 |
bytes.extend_from_slice(&LONG[..i]); |
| 605 |
bytes.extend_from_slice(&LONG[i..]); |
| 606 |
assert_eq!(LONG[..], *bytes); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
#[test] |
| 611 |
fn extend_mut_from_bytes() { |
| 612 |
let mut bytes = BytesMut::with_capacity(0); |
| 613 |
bytes.extend([Bytes::from(LONG)]); |
| 614 |
assert_eq!(*bytes, LONG[..]); |
| 615 |
} |
| 616 |
|
| 617 |
#[test] |
| 618 |
fn extend_past_lower_limit_of_size_hint() { |
| 619 |
// See https://github.com/tokio-rs/bytes/pull/674#pullrequestreview-1913035700 |
| 620 |
struct Iter<I>(I); |
| 621 |
|
| 622 |
impl<I: Iterator<Item = u8>> Iterator for Iter<I> { |
| 623 |
type Item = u8; |
| 624 |
|
| 625 |
fn next(&mut self) -> Option<Self::Item> { |
| 626 |
self.0.next() |
| 627 |
} |
| 628 |
|
| 629 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 630 |
(5, None) |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
let mut bytes = BytesMut::with_capacity(5); |
| 635 |
bytes.extend(Iter(std::iter::repeat(0).take(10))); |
| 636 |
assert_eq!(bytes.len(), 10); |
| 637 |
} |
| 638 |
|
| 639 |
#[test] |
| 640 |
fn extend_mut_without_size_hint() { |
| 641 |
let mut bytes = BytesMut::with_capacity(0); |
| 642 |
let mut long_iter = LONG.iter(); |
| 643 |
|
| 644 |
// Use iter::from_fn since it doesn't know a size_hint |
| 645 |
bytes.extend(std::iter::from_fn(|| long_iter.next())); |
| 646 |
assert_eq!(*bytes, LONG[..]); |
| 647 |
} |
| 648 |
|
| 649 |
#[test] |
| 650 |
fn from_static() { |
| 651 |
let mut a = Bytes::from_static(b"ab"); |
| 652 |
let b = a.split_off(1); |
| 653 |
|
| 654 |
assert_eq!(a, b"a"[..]); |
| 655 |
assert_eq!(b, b"b"[..]); |
| 656 |
} |
| 657 |
|
| 658 |
#[test] |
| 659 |
fn advance_static() { |
| 660 |
let mut a = Bytes::from_static(b"hello world"); |
| 661 |
a.advance(6); |
| 662 |
assert_eq!(a, &b"world"[..]); |
| 663 |
} |
| 664 |
|
| 665 |
#[test] |
| 666 |
fn advance_vec() { |
| 667 |
let mut a = Bytes::from(b"hello world boooo yah world zomg wat wat".to_vec()); |
| 668 |
a.advance(16); |
| 669 |
assert_eq!(a, b"o yah world zomg wat wat"[..]); |
| 670 |
|
| 671 |
a.advance(4); |
| 672 |
assert_eq!(a, b"h world zomg wat wat"[..]); |
| 673 |
|
| 674 |
a.advance(6); |
| 675 |
assert_eq!(a, b"d zomg wat wat"[..]); |
| 676 |
} |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn advance_bytes_mut() { |
| 680 |
let mut a = BytesMut::from("hello world boooo yah world zomg wat wat"); |
| 681 |
a.advance(16); |
| 682 |
assert_eq!(a, b"o yah world zomg wat wat"[..]); |
| 683 |
|
| 684 |
a.advance(4); |
| 685 |
assert_eq!(a, b"h world zomg wat wat"[..]); |
| 686 |
|
| 687 |
// Reserve some space. |
| 688 |
a.reserve(1024); |
| 689 |
assert_eq!(a, b"h world zomg wat wat"[..]); |
| 690 |
|
| 691 |
a.advance(6); |
| 692 |
assert_eq!(a, b"d zomg wat wat"[..]); |
| 693 |
} |
| 694 |
|
| 695 |
// Ensures BytesMut::advance reduces always capacity |
| 696 |
// |
| 697 |
// See https://github.com/tokio-rs/bytes/issues/725 |
| 698 |
#[test] |
| 699 |
fn advance_bytes_mut_remaining_capacity() { |
| 700 |
// reduce the search space under miri |
| 701 |
let max_capacity = if cfg!(miri) { 16 } else { 256 }; |
| 702 |
for capacity in 0..=max_capacity { |
| 703 |
for len in 0..=capacity { |
| 704 |
for advance in 0..=len { |
| 705 |
eprintln!("testing capacity={capacity}, len={len}, advance={advance}"); |
| 706 |
let mut buf = BytesMut::with_capacity(capacity); |
| 707 |
|
| 708 |
buf.resize(len, 42); |
| 709 |
assert_eq!(buf.len(), len, "resize should write `len` bytes"); |
| 710 |
assert_eq!( |
| 711 |
buf.remaining(), |
| 712 |
len, |
| 713 |
"Buf::remaining() should equal BytesMut::len" |
| 714 |
); |
| 715 |
|
| 716 |
buf.advance(advance); |
| 717 |
assert_eq!( |
| 718 |
buf.remaining(), |
| 719 |
len - advance, |
| 720 |
"Buf::advance should reduce the remaining len" |
| 721 |
); |
| 722 |
assert_eq!( |
| 723 |
buf.capacity(), |
| 724 |
capacity - advance, |
| 725 |
"Buf::advance should reduce the remaining capacity" |
| 726 |
); |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
#[test] |
| 733 |
#[should_panic] |
| 734 |
fn advance_past_len() { |
| 735 |
let mut a = BytesMut::from("hello world"); |
| 736 |
a.advance(20); |
| 737 |
} |
| 738 |
|
| 739 |
#[test] |
| 740 |
#[should_panic] |
| 741 |
fn mut_advance_past_len() { |
| 742 |
let mut a = BytesMut::from("hello world"); |
| 743 |
unsafe { |
| 744 |
a.advance_mut(20); |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
#[test] |
| 749 |
// Only run these tests on little endian systems. CI uses qemu for testing |
| 750 |
// big endian... and qemu doesn't really support threading all that well. |
| 751 |
#[cfg(any(miri, target_endian = "little"))] |
| 752 |
#[cfg(not(target_family = "wasm"))] // wasm without experimental threads proposal doesn't support threads |
| 753 |
fn stress() { |
| 754 |
// Tests promoting a buffer from a vec -> shared in a concurrent situation |
| 755 |
use std::sync::{Arc, Barrier}; |
| 756 |
use std::thread; |
| 757 |
|
| 758 |
const THREADS: usize = 8; |
| 759 |
const ITERS: usize = if cfg!(miri) { 100 } else { 1_000 }; |
| 760 |
|
| 761 |
for i in 0..ITERS { |
| 762 |
let data = [i as u8; 256]; |
| 763 |
let buf = Arc::new(Bytes::copy_from_slice(&data[..])); |
| 764 |
|
| 765 |
let barrier = Arc::new(Barrier::new(THREADS)); |
| 766 |
let mut joins = Vec::with_capacity(THREADS); |
| 767 |
|
| 768 |
for _ in 0..THREADS { |
| 769 |
let c = barrier.clone(); |
| 770 |
let buf = buf.clone(); |
| 771 |
|
| 772 |
joins.push(thread::spawn(move || { |
| 773 |
c.wait(); |
| 774 |
let buf: Bytes = (*buf).clone(); |
| 775 |
drop(buf); |
| 776 |
})); |
| 777 |
} |
| 778 |
|
| 779 |
for th in joins { |
| 780 |
th.join().unwrap(); |
| 781 |
} |
| 782 |
|
| 783 |
assert_eq!(*buf, data[..]); |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
#[test] |
| 788 |
fn partial_eq_bytesmut() { |
| 789 |
let bytes = Bytes::from(&b"The quick red fox"[..]); |
| 790 |
let bytesmut = BytesMut::from(&b"The quick red fox"[..]); |
| 791 |
assert!(bytes == bytesmut); |
| 792 |
assert!(bytesmut == bytes); |
| 793 |
let bytes2 = Bytes::from(&b"Jumped over the lazy brown dog"[..]); |
| 794 |
assert!(bytes2 != bytesmut); |
| 795 |
assert!(bytesmut != bytes2); |
| 796 |
} |
| 797 |
|
| 798 |
#[test] |
| 799 |
fn bytes_mut_unsplit_basic() { |
| 800 |
let mut buf = BytesMut::with_capacity(64); |
| 801 |
buf.extend_from_slice(b"aaabbbcccddd"); |
| 802 |
|
| 803 |
let splitted = buf.split_off(6); |
| 804 |
assert_eq!(b"aaabbb", &buf[..]); |
| 805 |
assert_eq!(b"cccddd", &splitted[..]); |
| 806 |
|
| 807 |
buf.unsplit(splitted); |
| 808 |
assert_eq!(b"aaabbbcccddd", &buf[..]); |
| 809 |
} |
| 810 |
|
| 811 |
#[test] |
| 812 |
fn bytes_mut_unsplit_empty_other() { |
| 813 |
let mut buf = BytesMut::with_capacity(64); |
| 814 |
buf.extend_from_slice(b"aaabbbcccddd"); |
| 815 |
|
| 816 |
// empty other |
| 817 |
let other = BytesMut::new(); |
| 818 |
|
| 819 |
buf.unsplit(other); |
| 820 |
assert_eq!(b"aaabbbcccddd", &buf[..]); |
| 821 |
} |
| 822 |
|
| 823 |
#[test] |
| 824 |
fn bytes_mut_unsplit_empty_self() { |
| 825 |
// empty self |
| 826 |
let mut buf = BytesMut::new(); |
| 827 |
|
| 828 |
let mut other = BytesMut::with_capacity(64); |
| 829 |
other.extend_from_slice(b"aaabbbcccddd"); |
| 830 |
|
| 831 |
buf.unsplit(other); |
| 832 |
assert_eq!(b"aaabbbcccddd", &buf[..]); |
| 833 |
} |
| 834 |
|
| 835 |
#[test] |
| 836 |
fn bytes_mut_unsplit_other_keeps_capacity() { |
| 837 |
let mut buf = BytesMut::with_capacity(64); |
| 838 |
buf.extend_from_slice(b"aabb"); |
| 839 |
|
| 840 |
// non empty other created "from" buf |
| 841 |
let mut other = buf.split_off(buf.len()); |
| 842 |
other.extend_from_slice(b"ccddee"); |
| 843 |
buf.unsplit(other); |
| 844 |
|
| 845 |
assert_eq!(buf.capacity(), 64); |
| 846 |
} |
| 847 |
|
| 848 |
#[test] |
| 849 |
fn bytes_mut_unsplit_empty_other_keeps_capacity() { |
| 850 |
let mut buf = BytesMut::with_capacity(64); |
| 851 |
buf.extend_from_slice(b"aabbccddee"); |
| 852 |
|
| 853 |
// empty other created "from" buf |
| 854 |
let other = buf.split_off(buf.len()); |
| 855 |
buf.unsplit(other); |
| 856 |
|
| 857 |
assert_eq!(buf.capacity(), 64); |
| 858 |
} |
| 859 |
|
| 860 |
#[test] |
| 861 |
fn bytes_mut_unsplit_arc_different() { |
| 862 |
let mut buf = BytesMut::with_capacity(64); |
| 863 |
buf.extend_from_slice(b"aaaabbbbeeee"); |
| 864 |
|
| 865 |
let _ = buf.split_off(8); //arc |
| 866 |
|
| 867 |
let mut buf2 = BytesMut::with_capacity(64); |
| 868 |
buf2.extend_from_slice(b"ccccddddeeee"); |
| 869 |
|
| 870 |
let _ = buf2.split_off(8); //arc |
| 871 |
|
| 872 |
buf.unsplit(buf2); |
| 873 |
assert_eq!(b"aaaabbbbccccdddd", &buf[..]); |
| 874 |
} |
| 875 |
|
| 876 |
#[test] |
| 877 |
fn bytes_mut_unsplit_arc_non_contiguous() { |
| 878 |
let mut buf = BytesMut::with_capacity(64); |
| 879 |
buf.extend_from_slice(b"aaaabbbbeeeeccccdddd"); |
| 880 |
|
| 881 |
let mut buf2 = buf.split_off(8); //arc |
| 882 |
|
| 883 |
let buf3 = buf2.split_off(4); //arc |
| 884 |
|
| 885 |
buf.unsplit(buf3); |
| 886 |
assert_eq!(b"aaaabbbbccccdddd", &buf[..]); |
| 887 |
} |
| 888 |
|
| 889 |
#[test] |
| 890 |
fn bytes_mut_unsplit_two_split_offs() { |
| 891 |
let mut buf = BytesMut::with_capacity(64); |
| 892 |
buf.extend_from_slice(b"aaaabbbbccccdddd"); |
| 893 |
|
| 894 |
let mut buf2 = buf.split_off(8); //arc |
| 895 |
let buf3 = buf2.split_off(4); //arc |
| 896 |
|
| 897 |
buf2.unsplit(buf3); |
| 898 |
buf.unsplit(buf2); |
| 899 |
assert_eq!(b"aaaabbbbccccdddd", &buf[..]); |
| 900 |
} |
| 901 |
|
| 902 |
#[test] |
| 903 |
fn from_iter_no_size_hint() { |
| 904 |
use std::iter; |
| 905 |
|
| 906 |
let mut expect = vec![]; |
| 907 |
|
| 908 |
let actual: Bytes = iter::repeat(b'x') |
| 909 |
.scan(100, |cnt, item| { |
| 910 |
if *cnt >= 1 { |
| 911 |
*cnt -= 1; |
| 912 |
expect.push(item); |
| 913 |
Some(item) |
| 914 |
} else { |
| 915 |
None |
| 916 |
} |
| 917 |
}) |
| 918 |
.collect(); |
| 919 |
|
| 920 |
assert_eq!(&actual[..], &expect[..]); |
| 921 |
} |
| 922 |
|
| 923 |
fn test_slice_ref(bytes: &Bytes, start: usize, end: usize, expected: &[u8]) { |
| 924 |
let slice = &(bytes.as_ref()[start..end]); |
| 925 |
let sub = bytes.slice_ref(slice); |
| 926 |
assert_eq!(&sub[..], expected); |
| 927 |
} |
| 928 |
|
| 929 |
#[test] |
| 930 |
fn slice_ref_works() { |
| 931 |
let bytes = Bytes::from(&b"012345678"[..]); |
| 932 |
|
| 933 |
test_slice_ref(&bytes, 0, 0, b""); |
| 934 |
test_slice_ref(&bytes, 0, 3, b"012"); |
| 935 |
test_slice_ref(&bytes, 2, 6, b"2345"); |
| 936 |
test_slice_ref(&bytes, 7, 9, b"78"); |
| 937 |
test_slice_ref(&bytes, 9, 9, b""); |
| 938 |
} |
| 939 |
|
| 940 |
#[test] |
| 941 |
fn slice_ref_empty() { |
| 942 |
let bytes = Bytes::from(&b""[..]); |
| 943 |
let slice = &(bytes.as_ref()[0..0]); |
| 944 |
|
| 945 |
let sub = bytes.slice_ref(slice); |
| 946 |
assert_eq!(&sub[..], b""); |
| 947 |
} |
| 948 |
|
| 949 |
#[test] |
| 950 |
fn slice_ref_empty_subslice() { |
| 951 |
let bytes = Bytes::from(&b"abcde"[..]); |
| 952 |
let subbytes = bytes.slice(0..0); |
| 953 |
let slice = &subbytes[..]; |
| 954 |
// The `slice` object is derived from the original `bytes` object |
| 955 |
// so `slice_ref` should work. |
| 956 |
assert_eq!(Bytes::new(), bytes.slice_ref(slice)); |
| 957 |
} |
| 958 |
|
| 959 |
#[test] |
| 960 |
#[should_panic] |
| 961 |
fn slice_ref_catches_not_a_subset() { |
| 962 |
let bytes = Bytes::from(&b"012345678"[..]); |
| 963 |
let slice = &b"012345"[0..4]; |
| 964 |
|
| 965 |
bytes.slice_ref(slice); |
| 966 |
} |
| 967 |
|
| 968 |
#[test] |
| 969 |
fn slice_ref_not_an_empty_subset() { |
| 970 |
let bytes = Bytes::from(&b"012345678"[..]); |
| 971 |
let slice = &b""[0..0]; |
| 972 |
|
| 973 |
assert_eq!(Bytes::new(), bytes.slice_ref(slice)); |
| 974 |
} |
| 975 |
|
| 976 |
#[test] |
| 977 |
fn empty_slice_ref_not_an_empty_subset() { |
| 978 |
let bytes = Bytes::new(); |
| 979 |
let slice = &b"some other slice"[0..0]; |
| 980 |
|
| 981 |
assert_eq!(Bytes::new(), bytes.slice_ref(slice)); |
| 982 |
} |
| 983 |
|
| 984 |
#[test] |
| 985 |
fn bytes_buf_mut_advance() { |
| 986 |
let mut bytes = BytesMut::with_capacity(1024); |
| 987 |
|
| 988 |
unsafe { |
| 989 |
let ptr = bytes.chunk_mut().as_mut_ptr(); |
| 990 |
assert_eq!(1024, bytes.chunk_mut().len()); |
| 991 |
|
| 992 |
bytes.advance_mut(10); |
| 993 |
|
| 994 |
let next = bytes.chunk_mut().as_mut_ptr(); |
| 995 |
assert_eq!(1024 - 10, bytes.chunk_mut().len()); |
| 996 |
assert_eq!(ptr.offset(10), next); |
| 997 |
|
| 998 |
// advance to the end |
| 999 |
bytes.advance_mut(1024 - 10); |
| 1000 |
|
| 1001 |
// The buffer size is doubled |
| 1002 |
assert_eq!(1024, bytes.chunk_mut().len()); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
#[test] |
| 1007 |
fn bytes_buf_mut_reuse_when_fully_consumed() { |
| 1008 |
use bytes::{Buf, BytesMut}; |
| 1009 |
let mut buf = BytesMut::new(); |
| 1010 |
buf.reserve(8192); |
| 1011 |
buf.extend_from_slice(&[0u8; 100][..]); |
| 1012 |
|
| 1013 |
let p = &buf[0] as *const u8; |
| 1014 |
buf.advance(100); |
| 1015 |
|
| 1016 |
buf.reserve(8192); |
| 1017 |
buf.extend_from_slice(b" "); |
| 1018 |
|
| 1019 |
assert_eq!(&buf[0] as *const u8, p); |
| 1020 |
} |
| 1021 |
|
| 1022 |
#[test] |
| 1023 |
#[should_panic] |
| 1024 |
fn bytes_reserve_overflow() { |
| 1025 |
let mut bytes = BytesMut::with_capacity(1024); |
| 1026 |
bytes.put_slice(b"hello world"); |
| 1027 |
|
| 1028 |
bytes.reserve(usize::MAX); |
| 1029 |
} |
| 1030 |
|
| 1031 |
#[test] |
| 1032 |
fn bytes_with_capacity_but_empty() { |
| 1033 |
// See https://github.com/tokio-rs/bytes/issues/340 |
| 1034 |
let vec = Vec::with_capacity(1); |
| 1035 |
let _ = Bytes::from(vec); |
| 1036 |
} |
| 1037 |
|
| 1038 |
#[test] |
| 1039 |
fn bytes_put_bytes() { |
| 1040 |
let mut bytes = BytesMut::new(); |
| 1041 |
bytes.put_u8(17); |
| 1042 |
bytes.put_bytes(19, 2); |
| 1043 |
assert_eq!([17, 19, 19], bytes.as_ref()); |
| 1044 |
} |
| 1045 |
|
| 1046 |
#[test] |
| 1047 |
fn box_slice_empty() { |
| 1048 |
// See https://github.com/tokio-rs/bytes/issues/340 |
| 1049 |
let empty: Box<[u8]> = Default::default(); |
| 1050 |
let b = Bytes::from(empty); |
| 1051 |
assert!(b.is_empty()); |
| 1052 |
} |
| 1053 |
|
| 1054 |
#[test] |
| 1055 |
fn bytes_into_vec() { |
| 1056 |
// Test kind == KIND_VEC |
| 1057 |
let content = b"helloworld"; |
| 1058 |
|
| 1059 |
let mut bytes = BytesMut::new(); |
| 1060 |
bytes.put_slice(content); |
| 1061 |
|
| 1062 |
let vec: Vec<u8> = bytes.into(); |
| 1063 |
assert_eq!(&vec, content); |
| 1064 |
|
| 1065 |
// Test kind == KIND_ARC, shared.is_unique() == True |
| 1066 |
let mut bytes = BytesMut::new(); |
| 1067 |
bytes.put_slice(b"abcdewe23"); |
| 1068 |
bytes.put_slice(content); |
| 1069 |
|
| 1070 |
// Overwrite the bytes to make sure only one reference to the underlying |
| 1071 |
// Vec exists. |
| 1072 |
bytes = bytes.split_off(9); |
| 1073 |
|
| 1074 |
let vec: Vec<u8> = bytes.into(); |
| 1075 |
assert_eq!(&vec, content); |
| 1076 |
|
| 1077 |
// Test kind == KIND_ARC, shared.is_unique() == False |
| 1078 |
let prefix = b"abcdewe23"; |
| 1079 |
|
| 1080 |
let mut bytes = BytesMut::new(); |
| 1081 |
bytes.put_slice(prefix); |
| 1082 |
bytes.put_slice(content); |
| 1083 |
|
| 1084 |
let vec: Vec<u8> = bytes.split_off(prefix.len()).into(); |
| 1085 |
assert_eq!(&vec, content); |
| 1086 |
|
| 1087 |
let vec: Vec<u8> = bytes.into(); |
| 1088 |
assert_eq!(&vec, prefix); |
| 1089 |
} |
| 1090 |
|
| 1091 |
#[test] |
| 1092 |
fn test_bytes_into_vec() { |
| 1093 |
// Test STATIC_VTABLE.to_vec |
| 1094 |
let bs = b"1b23exfcz3r"; |
| 1095 |
let vec: Vec<u8> = Bytes::from_static(bs).into(); |
| 1096 |
assert_eq!(&*vec, bs); |
| 1097 |
|
| 1098 |
// Test bytes_mut.SHARED_VTABLE.to_vec impl |
| 1099 |
eprintln!("1"); |
| 1100 |
let mut bytes_mut: BytesMut = bs[..].into(); |
| 1101 |
|
| 1102 |
// Set kind to KIND_ARC so that after freeze, Bytes will use bytes_mut.SHARED_VTABLE |
| 1103 |
eprintln!("2"); |
| 1104 |
drop(bytes_mut.split_off(bs.len())); |
| 1105 |
|
| 1106 |
eprintln!("3"); |
| 1107 |
let b1 = bytes_mut.freeze(); |
| 1108 |
eprintln!("4"); |
| 1109 |
let b2 = b1.clone(); |
| 1110 |
|
| 1111 |
eprintln!("{:#?}", (&*b1).as_ptr()); |
| 1112 |
|
| 1113 |
// shared.is_unique() = False |
| 1114 |
eprintln!("5"); |
| 1115 |
assert_eq!(&*Vec::from(b2), bs); |
| 1116 |
|
| 1117 |
// shared.is_unique() = True |
| 1118 |
eprintln!("6"); |
| 1119 |
assert_eq!(&*Vec::from(b1), bs); |
| 1120 |
|
| 1121 |
// Test bytes_mut.SHARED_VTABLE.to_vec impl where offset != 0 |
| 1122 |
let mut bytes_mut1: BytesMut = bs[..].into(); |
| 1123 |
let bytes_mut2 = bytes_mut1.split_off(9); |
| 1124 |
|
| 1125 |
let b1 = bytes_mut1.freeze(); |
| 1126 |
let b2 = bytes_mut2.freeze(); |
| 1127 |
|
| 1128 |
assert_eq!(Vec::from(b2), bs[9..]); |
| 1129 |
assert_eq!(Vec::from(b1), bs[..9]); |
| 1130 |
} |
| 1131 |
|
| 1132 |
#[test] |
| 1133 |
fn test_bytes_into_vec_promotable_even() { |
| 1134 |
let vec = vec![33u8; 1024]; |
| 1135 |
|
| 1136 |
// Test cases where kind == KIND_VEC |
| 1137 |
let b1 = Bytes::from(vec.clone()); |
| 1138 |
assert_eq!(Vec::from(b1), vec); |
| 1139 |
|
| 1140 |
// Test cases where kind == KIND_ARC, ref_cnt == 1 |
| 1141 |
let b1 = Bytes::from(vec.clone()); |
| 1142 |
drop(b1.clone()); |
| 1143 |
assert_eq!(Vec::from(b1), vec); |
| 1144 |
|
| 1145 |
// Test cases where kind == KIND_ARC, ref_cnt == 2 |
| 1146 |
let b1 = Bytes::from(vec.clone()); |
| 1147 |
let b2 = b1.clone(); |
| 1148 |
assert_eq!(Vec::from(b1), vec); |
| 1149 |
|
| 1150 |
// Test cases where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1 |
| 1151 |
assert_eq!(Vec::from(b2), vec); |
| 1152 |
|
| 1153 |
// Test cases where offset != 0 |
| 1154 |
let mut b1 = Bytes::from(vec.clone()); |
| 1155 |
let b2 = b1.split_off(20); |
| 1156 |
|
| 1157 |
assert_eq!(Vec::from(b2), vec[20..]); |
| 1158 |
assert_eq!(Vec::from(b1), vec[..20]); |
| 1159 |
} |
| 1160 |
|
| 1161 |
#[test] |
| 1162 |
fn test_bytes_vec_conversion() { |
| 1163 |
let mut vec = Vec::with_capacity(10); |
| 1164 |
vec.extend(b"abcdefg"); |
| 1165 |
let b = Bytes::from(vec); |
| 1166 |
let v = Vec::from(b); |
| 1167 |
assert_eq!(v.len(), 7); |
| 1168 |
assert_eq!(v.capacity(), 10); |
| 1169 |
|
| 1170 |
let mut b = Bytes::from(v); |
| 1171 |
b.advance(1); |
| 1172 |
let v = Vec::from(b); |
| 1173 |
assert_eq!(v.len(), 6); |
| 1174 |
assert_eq!(v.capacity(), 10); |
| 1175 |
assert_eq!(v.as_slice(), b"bcdefg"); |
| 1176 |
} |
| 1177 |
|
| 1178 |
#[test] |
| 1179 |
fn test_bytes_mut_conversion() { |
| 1180 |
let mut b1 = BytesMut::with_capacity(10); |
| 1181 |
b1.extend(b"abcdefg"); |
| 1182 |
let b2 = Bytes::from(b1); |
| 1183 |
let v = Vec::from(b2); |
| 1184 |
assert_eq!(v.len(), 7); |
| 1185 |
assert_eq!(v.capacity(), 10); |
| 1186 |
|
| 1187 |
let mut b = Bytes::from(v); |
| 1188 |
b.advance(1); |
| 1189 |
let v = Vec::from(b); |
| 1190 |
assert_eq!(v.len(), 6); |
| 1191 |
assert_eq!(v.capacity(), 10); |
| 1192 |
assert_eq!(v.as_slice(), b"bcdefg"); |
| 1193 |
} |
| 1194 |
|
| 1195 |
#[test] |
| 1196 |
fn test_bytes_capacity_len() { |
| 1197 |
for cap in 0..100 { |
| 1198 |
for len in 0..=cap { |
| 1199 |
let mut v = Vec::with_capacity(cap); |
| 1200 |
v.resize(len, 0); |
| 1201 |
let _ = Bytes::from(v); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
#[test] |
| 1207 |
fn static_is_unique() { |
| 1208 |
let b = Bytes::from_static(LONG); |
| 1209 |
assert!(!b.is_unique()); |
| 1210 |
} |
| 1211 |
|
| 1212 |
#[test] |
| 1213 |
fn vec_is_unique() { |
| 1214 |
let v: Vec<u8> = LONG.to_vec(); |
| 1215 |
let b = Bytes::from(v); |
| 1216 |
assert!(b.is_unique()); |
| 1217 |
} |
| 1218 |
|
| 1219 |
#[test] |
| 1220 |
fn arc_is_unique() { |
| 1221 |
let v: Vec<u8> = LONG.to_vec(); |
| 1222 |
let b = Bytes::from(v); |
| 1223 |
let c = b.clone(); |
| 1224 |
assert!(!b.is_unique()); |
| 1225 |
drop(c); |
| 1226 |
assert!(b.is_unique()); |
| 1227 |
} |
| 1228 |
|
| 1229 |
#[test] |
| 1230 |
fn shared_is_unique() { |
| 1231 |
let v: Vec<u8> = LONG.to_vec(); |
| 1232 |
let b = Bytes::from(v); |
| 1233 |
let c = b.clone(); |
| 1234 |
assert!(!c.is_unique()); |
| 1235 |
drop(b); |
| 1236 |
assert!(c.is_unique()); |
| 1237 |
} |
| 1238 |
|
| 1239 |
#[test] |
| 1240 |
fn mut_shared_is_unique() { |
| 1241 |
let mut b = BytesMut::from(LONG); |
| 1242 |
let c = b.split().freeze(); |
| 1243 |
assert!(!c.is_unique()); |
| 1244 |
drop(b); |
| 1245 |
assert!(c.is_unique()); |
| 1246 |
} |
| 1247 |
|
| 1248 |
#[test] |
| 1249 |
fn test_bytesmut_from_bytes_static() { |
| 1250 |
let bs = b"1b23exfcz3r"; |
| 1251 |
|
| 1252 |
// Test STATIC_VTABLE.to_mut |
| 1253 |
let bytes_mut = BytesMut::from(Bytes::from_static(bs)); |
| 1254 |
assert_eq!(bytes_mut, bs[..]); |
| 1255 |
} |
| 1256 |
|
| 1257 |
#[test] |
| 1258 |
fn test_bytesmut_from_bytes_bytes_mut_vec() { |
| 1259 |
let bs = b"1b23exfcz3r"; |
| 1260 |
let bs_long = b"1b23exfcz3r1b23exfcz3r"; |
| 1261 |
|
| 1262 |
// Test case where kind == KIND_VEC |
| 1263 |
let mut bytes_mut: BytesMut = bs[..].into(); |
| 1264 |
bytes_mut = BytesMut::from(bytes_mut.freeze()); |
| 1265 |
assert_eq!(bytes_mut, bs[..]); |
| 1266 |
bytes_mut.extend_from_slice(&bs[..]); |
| 1267 |
assert_eq!(bytes_mut, bs_long[..]); |
| 1268 |
} |
| 1269 |
|
| 1270 |
#[test] |
| 1271 |
fn test_bytesmut_from_bytes_bytes_mut_shared() { |
| 1272 |
let bs = b"1b23exfcz3r"; |
| 1273 |
|
| 1274 |
// Set kind to KIND_ARC so that after freeze, Bytes will use bytes_mut.SHARED_VTABLE |
| 1275 |
let mut bytes_mut: BytesMut = bs[..].into(); |
| 1276 |
drop(bytes_mut.split_off(bs.len())); |
| 1277 |
|
| 1278 |
let b1 = bytes_mut.freeze(); |
| 1279 |
let b2 = b1.clone(); |
| 1280 |
|
| 1281 |
// shared.is_unique() = False |
| 1282 |
let mut b1m = BytesMut::from(b1); |
| 1283 |
assert_eq!(b1m, bs[..]); |
| 1284 |
b1m[0] = b'9'; |
| 1285 |
|
| 1286 |
// shared.is_unique() = True |
| 1287 |
let b2m = BytesMut::from(b2); |
| 1288 |
assert_eq!(b2m, bs[..]); |
| 1289 |
} |
| 1290 |
|
| 1291 |
#[test] |
| 1292 |
fn test_bytesmut_from_bytes_bytes_mut_offset() { |
| 1293 |
let bs = b"1b23exfcz3r"; |
| 1294 |
|
| 1295 |
// Test bytes_mut.SHARED_VTABLE.to_mut impl where offset != 0 |
| 1296 |
let mut bytes_mut1: BytesMut = bs[..].into(); |
| 1297 |
let bytes_mut2 = bytes_mut1.split_off(9); |
| 1298 |
|
| 1299 |
let b1 = bytes_mut1.freeze(); |
| 1300 |
let b2 = bytes_mut2.freeze(); |
| 1301 |
|
| 1302 |
let b1m = BytesMut::from(b1); |
| 1303 |
let b2m = BytesMut::from(b2); |
| 1304 |
|
| 1305 |
assert_eq!(b2m, bs[9..]); |
| 1306 |
assert_eq!(b1m, bs[..9]); |
| 1307 |
} |
| 1308 |
|
| 1309 |
#[test] |
| 1310 |
fn test_bytesmut_from_bytes_promotable_even_vec() { |
| 1311 |
let vec = vec![33u8; 1024]; |
| 1312 |
|
| 1313 |
// Test case where kind == KIND_VEC |
| 1314 |
let b1 = Bytes::from(vec.clone()); |
| 1315 |
let b1m = BytesMut::from(b1); |
| 1316 |
assert_eq!(b1m, vec); |
| 1317 |
} |
| 1318 |
|
| 1319 |
#[test] |
| 1320 |
fn test_bytesmut_from_bytes_promotable_even_arc_1() { |
| 1321 |
let vec = vec![33u8; 1024]; |
| 1322 |
|
| 1323 |
// Test case where kind == KIND_ARC, ref_cnt == 1 |
| 1324 |
let b1 = Bytes::from(vec.clone()); |
| 1325 |
drop(b1.clone()); |
| 1326 |
let b1m = BytesMut::from(b1); |
| 1327 |
assert_eq!(b1m, vec); |
| 1328 |
} |
| 1329 |
|
| 1330 |
#[test] |
| 1331 |
fn test_bytesmut_from_bytes_promotable_even_arc_2() { |
| 1332 |
let vec = vec![33u8; 1024]; |
| 1333 |
|
| 1334 |
// Test case where kind == KIND_ARC, ref_cnt == 2 |
| 1335 |
let b1 = Bytes::from(vec.clone()); |
| 1336 |
let b2 = b1.clone(); |
| 1337 |
let b1m = BytesMut::from(b1); |
| 1338 |
assert_eq!(b1m, vec); |
| 1339 |
|
| 1340 |
// Test case where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1 |
| 1341 |
let b2m = BytesMut::from(b2); |
| 1342 |
assert_eq!(b2m, vec); |
| 1343 |
} |
| 1344 |
|
| 1345 |
#[test] |
| 1346 |
fn test_bytesmut_from_bytes_promotable_even_arc_offset() { |
| 1347 |
let vec = vec![33u8; 1024]; |
| 1348 |
|
| 1349 |
// Test case where offset != 0 |
| 1350 |
let mut b1 = Bytes::from(vec.clone()); |
| 1351 |
let b2 = b1.split_off(20); |
| 1352 |
let b1m = BytesMut::from(b1); |
| 1353 |
let b2m = BytesMut::from(b2); |
| 1354 |
|
| 1355 |
assert_eq!(b2m, vec[20..]); |
| 1356 |
assert_eq!(b1m, vec[..20]); |
| 1357 |
} |
| 1358 |
|
| 1359 |
#[test] |
| 1360 |
fn try_reclaim_empty() { |
| 1361 |
let mut buf = BytesMut::new(); |
| 1362 |
assert_eq!(false, buf.try_reclaim(6)); |
| 1363 |
buf.reserve(6); |
| 1364 |
assert_eq!(true, buf.try_reclaim(6)); |
| 1365 |
let cap = buf.capacity(); |
| 1366 |
assert!(cap >= 6); |
| 1367 |
assert_eq!(false, buf.try_reclaim(cap + 1)); |
| 1368 |
|
| 1369 |
let mut buf = BytesMut::new(); |
| 1370 |
buf.reserve(6); |
| 1371 |
let cap = buf.capacity(); |
| 1372 |
assert!(cap >= 6); |
| 1373 |
let mut split = buf.split(); |
| 1374 |
drop(buf); |
| 1375 |
assert_eq!(0, split.capacity()); |
| 1376 |
assert_eq!(true, split.try_reclaim(6)); |
| 1377 |
assert_eq!(false, split.try_reclaim(cap + 1)); |
| 1378 |
} |
| 1379 |
|
| 1380 |
#[test] |
| 1381 |
fn try_reclaim_vec() { |
| 1382 |
let mut buf = BytesMut::with_capacity(6); |
| 1383 |
buf.put_slice(b"abc"); |
| 1384 |
// Reclaiming a ludicrous amount of space should calmly return false |
| 1385 |
assert_eq!(false, buf.try_reclaim(usize::MAX)); |
| 1386 |
|
| 1387 |
assert_eq!(false, buf.try_reclaim(6)); |
| 1388 |
buf.advance(2); |
| 1389 |
assert_eq!(4, buf.capacity()); |
| 1390 |
// We can reclaim 5 bytes, because the byte in the buffer can be moved to the front. 6 bytes |
| 1391 |
// cannot be reclaimed because there is already one byte stored |
| 1392 |
assert_eq!(false, buf.try_reclaim(6)); |
| 1393 |
assert_eq!(true, buf.try_reclaim(5)); |
| 1394 |
buf.advance(1); |
| 1395 |
assert_eq!(true, buf.try_reclaim(6)); |
| 1396 |
assert_eq!(6, buf.capacity()); |
| 1397 |
} |
| 1398 |
|
| 1399 |
#[test] |
| 1400 |
fn try_reclaim_arc() { |
| 1401 |
let mut buf = BytesMut::with_capacity(6); |
| 1402 |
buf.put_slice(b"abc"); |
| 1403 |
let x = buf.split().freeze(); |
| 1404 |
buf.put_slice(b"def"); |
| 1405 |
// Reclaiming a ludicrous amount of space should calmly return false |
| 1406 |
assert_eq!(false, buf.try_reclaim(usize::MAX)); |
| 1407 |
|
| 1408 |
let y = buf.split().freeze(); |
| 1409 |
let z = y.clone(); |
| 1410 |
assert_eq!(false, buf.try_reclaim(6)); |
| 1411 |
drop(x); |
| 1412 |
drop(z); |
| 1413 |
assert_eq!(false, buf.try_reclaim(6)); |
| 1414 |
drop(y); |
| 1415 |
assert_eq!(true, buf.try_reclaim(6)); |
| 1416 |
assert_eq!(6, buf.capacity()); |
| 1417 |
assert_eq!(0, buf.len()); |
| 1418 |
buf.put_slice(b"abc"); |
| 1419 |
buf.put_slice(b"def"); |
| 1420 |
assert_eq!(6, buf.capacity()); |
| 1421 |
assert_eq!(6, buf.len()); |
| 1422 |
assert_eq!(false, buf.try_reclaim(6)); |
| 1423 |
buf.advance(4); |
| 1424 |
assert_eq!(true, buf.try_reclaim(4)); |
| 1425 |
buf.advance(2); |
| 1426 |
assert_eq!(true, buf.try_reclaim(6)); |
| 1427 |
} |
| 1428 |
|
| 1429 |
#[test] |
| 1430 |
fn slice_empty_addr() { |
| 1431 |
let buf = Bytes::from(vec![0; 1024]); |
| 1432 |
|
| 1433 |
let ptr_start = buf.as_ptr(); |
| 1434 |
let ptr_end = ptr_start.wrapping_add(1024); |
| 1435 |
|
| 1436 |
let empty_end = buf.slice(1024..); |
| 1437 |
assert_eq!(empty_end.len(), 0); |
| 1438 |
assert_eq!(empty_end.as_ptr(), ptr_end); |
| 1439 |
|
| 1440 |
let empty_start = buf.slice(..0); |
| 1441 |
assert_eq!(empty_start.len(), 0); |
| 1442 |
assert_eq!(empty_start.as_ptr(), ptr_start); |
| 1443 |
|
| 1444 |
// Is miri happy about the provenance? |
| 1445 |
let _ = &empty_end[..]; |
| 1446 |
let _ = &empty_start[..]; |
| 1447 |
} |
| 1448 |
|
| 1449 |
#[test] |
| 1450 |
fn split_off_empty_addr() { |
| 1451 |
let mut buf = Bytes::from(vec![0; 1024]); |
| 1452 |
|
| 1453 |
let ptr_start = buf.as_ptr(); |
| 1454 |
let ptr_end = ptr_start.wrapping_add(1024); |
| 1455 |
|
| 1456 |
let empty_end = buf.split_off(1024); |
| 1457 |
assert_eq!(empty_end.len(), 0); |
| 1458 |
assert_eq!(empty_end.as_ptr(), ptr_end); |
| 1459 |
|
| 1460 |
let _ = buf.split_off(0); |
| 1461 |
assert_eq!(buf.len(), 0); |
| 1462 |
assert_eq!(buf.as_ptr(), ptr_start); |
| 1463 |
|
| 1464 |
// Is miri happy about the provenance? |
| 1465 |
let _ = &empty_end[..]; |
| 1466 |
let _ = &buf[..]; |
| 1467 |
} |
| 1468 |
|
| 1469 |
#[test] |
| 1470 |
fn split_to_empty_addr() { |
| 1471 |
let mut buf = Bytes::from(vec![0; 1024]); |
| 1472 |
|
| 1473 |
let ptr_start = buf.as_ptr(); |
| 1474 |
let ptr_end = ptr_start.wrapping_add(1024); |
| 1475 |
|
| 1476 |
let empty_start = buf.split_to(0); |
| 1477 |
assert_eq!(empty_start.len(), 0); |
| 1478 |
assert_eq!(empty_start.as_ptr(), ptr_start); |
| 1479 |
|
| 1480 |
let _ = buf.split_to(1024); |
| 1481 |
assert_eq!(buf.len(), 0); |
| 1482 |
assert_eq!(buf.as_ptr(), ptr_end); |
| 1483 |
|
| 1484 |
// Is miri happy about the provenance? |
| 1485 |
let _ = &empty_start[..]; |
| 1486 |
let _ = &buf[..]; |
| 1487 |
} |
| 1488 |
|
| 1489 |
#[test] |
| 1490 |
fn split_off_empty_addr_mut() { |
| 1491 |
let mut buf = BytesMut::from([0; 1024].as_slice()); |
| 1492 |
|
| 1493 |
let ptr_start = buf.as_ptr(); |
| 1494 |
let ptr_end = ptr_start.wrapping_add(1024); |
| 1495 |
|
| 1496 |
let empty_end = buf.split_off(1024); |
| 1497 |
assert_eq!(empty_end.len(), 0); |
| 1498 |
assert_eq!(empty_end.as_ptr(), ptr_end); |
| 1499 |
|
| 1500 |
let _ = buf.split_off(0); |
| 1501 |
assert_eq!(buf.len(), 0); |
| 1502 |
assert_eq!(buf.as_ptr(), ptr_start); |
| 1503 |
|
| 1504 |
// Is miri happy about the provenance? |
| 1505 |
let _ = &empty_end[..]; |
| 1506 |
let _ = &buf[..]; |
| 1507 |
} |
| 1508 |
|
| 1509 |
#[test] |
| 1510 |
fn split_to_empty_addr_mut() { |
| 1511 |
let mut buf = BytesMut::from([0; 1024].as_slice()); |
| 1512 |
|
| 1513 |
let ptr_start = buf.as_ptr(); |
| 1514 |
let ptr_end = ptr_start.wrapping_add(1024); |
| 1515 |
|
| 1516 |
let empty_start = buf.split_to(0); |
| 1517 |
assert_eq!(empty_start.len(), 0); |
| 1518 |
assert_eq!(empty_start.as_ptr(), ptr_start); |
| 1519 |
|
| 1520 |
let _ = buf.split_to(1024); |
| 1521 |
assert_eq!(buf.len(), 0); |
| 1522 |
assert_eq!(buf.as_ptr(), ptr_end); |
| 1523 |
|
| 1524 |
// Is miri happy about the provenance? |
| 1525 |
let _ = &empty_start[..]; |
| 1526 |
let _ = &buf[..]; |
| 1527 |
} |
| 1528 |
|
| 1529 |
#[derive(Clone)] |
| 1530 |
struct SharedAtomicCounter(Arc<AtomicUsize>); |
| 1531 |
|
| 1532 |
impl SharedAtomicCounter { |
| 1533 |
pub fn new() -> Self { |
| 1534 |
SharedAtomicCounter(Arc::new(AtomicUsize::new(0))) |
| 1535 |
} |
| 1536 |
|
| 1537 |
pub fn increment(&self) { |
| 1538 |
self.0.fetch_add(1, Ordering::AcqRel); |
| 1539 |
} |
| 1540 |
|
| 1541 |
pub fn get(&self) -> usize { |
| 1542 |
self.0.load(Ordering::Acquire) |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
#[derive(Clone)] |
| 1547 |
struct OwnedTester<const L: usize> { |
| 1548 |
buf: [u8; L], |
| 1549 |
drop_count: SharedAtomicCounter, |
| 1550 |
pub panic_as_ref: bool, |
| 1551 |
} |
| 1552 |
|
| 1553 |
impl<const L: usize> OwnedTester<L> { |
| 1554 |
fn new(buf: [u8; L], drop_count: SharedAtomicCounter) -> Self { |
| 1555 |
Self { |
| 1556 |
buf, |
| 1557 |
drop_count, |
| 1558 |
panic_as_ref: false, |
| 1559 |
} |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
impl<const L: usize> AsRef<[u8]> for OwnedTester<L> { |
| 1564 |
fn as_ref(&self) -> &[u8] { |
| 1565 |
if self.panic_as_ref { |
| 1566 |
panic!("test-triggered panic in `AsRef<[u8]> for OwnedTester`"); |
| 1567 |
} |
| 1568 |
self.buf.as_slice() |
| 1569 |
} |
| 1570 |
} |
| 1571 |
|
| 1572 |
impl<const L: usize> Drop for OwnedTester<L> { |
| 1573 |
fn drop(&mut self) { |
| 1574 |
self.drop_count.increment(); |
| 1575 |
} |
| 1576 |
} |
| 1577 |
|
| 1578 |
#[test] |
| 1579 |
fn owned_is_unique_always_false() { |
| 1580 |
let b1 = Bytes::from_owner([1, 2, 3, 4, 5, 6, 7]); |
| 1581 |
assert!(!b1.is_unique()); // even if ref_cnt == 1 |
| 1582 |
let b2 = b1.clone(); |
| 1583 |
assert!(!b1.is_unique()); |
| 1584 |
assert!(!b2.is_unique()); |
| 1585 |
drop(b1); |
| 1586 |
assert!(!b2.is_unique()); // even if ref_cnt == 1 |
| 1587 |
} |
| 1588 |
|
| 1589 |
#[test] |
| 1590 |
fn owned_buf_sharing() { |
| 1591 |
let buf = [1, 2, 3, 4, 5, 6, 7]; |
| 1592 |
let b1 = Bytes::from_owner(buf); |
| 1593 |
let b2 = b1.clone(); |
| 1594 |
assert_eq!(&buf[..], &b1[..]); |
| 1595 |
assert_eq!(&buf[..], &b2[..]); |
| 1596 |
assert_eq!(b1.as_ptr(), b2.as_ptr()); |
| 1597 |
assert_eq!(b1.len(), b2.len()); |
| 1598 |
assert_eq!(b1.len(), buf.len()); |
| 1599 |
} |
| 1600 |
|
| 1601 |
#[test] |
| 1602 |
fn owned_buf_slicing() { |
| 1603 |
let b1 = Bytes::from_owner(SHORT); |
| 1604 |
assert_eq!(SHORT, &b1[..]); |
| 1605 |
let b2 = b1.slice(1..(b1.len() - 1)); |
| 1606 |
assert_eq!(&SHORT[1..(SHORT.len() - 1)], b2); |
| 1607 |
assert_eq!(unsafe { SHORT.as_ptr().add(1) }, b2.as_ptr()); |
| 1608 |
assert_eq!(SHORT.len() - 2, b2.len()); |
| 1609 |
} |
| 1610 |
|
| 1611 |
#[test] |
| 1612 |
fn owned_dropped_exactly_once() { |
| 1613 |
let buf: [u8; 5] = [1, 2, 3, 4, 5]; |
| 1614 |
let drop_counter = SharedAtomicCounter::new(); |
| 1615 |
let owner = OwnedTester::new(buf, drop_counter.clone()); |
| 1616 |
let b1 = Bytes::from_owner(owner); |
| 1617 |
let b2 = b1.clone(); |
| 1618 |
assert_eq!(drop_counter.get(), 0); |
| 1619 |
drop(b1); |
| 1620 |
assert_eq!(drop_counter.get(), 0); |
| 1621 |
let b3 = b2.slice(1..b2.len() - 1); |
| 1622 |
drop(b2); |
| 1623 |
assert_eq!(drop_counter.get(), 0); |
| 1624 |
drop(b3); |
| 1625 |
assert_eq!(drop_counter.get(), 1); |
| 1626 |
} |
| 1627 |
|
| 1628 |
#[test] |
| 1629 |
fn owned_to_mut() { |
| 1630 |
let buf: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 1631 |
let drop_counter = SharedAtomicCounter::new(); |
| 1632 |
let owner = OwnedTester::new(buf, drop_counter.clone()); |
| 1633 |
let b1 = Bytes::from_owner(owner); |
| 1634 |
|
| 1635 |
// Holding an owner will fail converting to a BytesMut, |
| 1636 |
// even when the bytes instance has a ref_cnt == 1. |
| 1637 |
let b1 = b1.try_into_mut().unwrap_err(); |
| 1638 |
|
| 1639 |
// That said, it's still possible, just not cheap. |
| 1640 |
let bm1: BytesMut = b1.into(); |
| 1641 |
let new_buf = &bm1[..]; |
| 1642 |
assert_eq!(new_buf, &buf[..]); |
| 1643 |
|
| 1644 |
// `.into::<BytesMut>()` has correctly dropped the owner |
| 1645 |
assert_eq!(drop_counter.get(), 1); |
| 1646 |
} |
| 1647 |
|
| 1648 |
#[test] |
| 1649 |
fn owned_to_vec() { |
| 1650 |
let buf: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 1651 |
let drop_counter = SharedAtomicCounter::new(); |
| 1652 |
let owner = OwnedTester::new(buf, drop_counter.clone()); |
| 1653 |
let b1 = Bytes::from_owner(owner); |
| 1654 |
|
| 1655 |
let v1 = b1.to_vec(); |
| 1656 |
assert_eq!(&v1[..], &buf[..]); |
| 1657 |
assert_eq!(&v1[..], &b1[..]); |
| 1658 |
|
| 1659 |
drop(b1); |
| 1660 |
assert_eq!(drop_counter.get(), 1); |
| 1661 |
} |
| 1662 |
|
| 1663 |
#[test] |
| 1664 |
fn owned_into_vec() { |
| 1665 |
let drop_counter = SharedAtomicCounter::new(); |
| 1666 |
let buf: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 1667 |
let owner = OwnedTester::new(buf, drop_counter.clone()); |
| 1668 |
let b1 = Bytes::from_owner(owner); |
| 1669 |
|
| 1670 |
let v1: Vec<u8> = b1.into(); |
| 1671 |
assert_eq!(&v1[..], &buf[..]); |
| 1672 |
// into() vec will copy out of the owner and drop it |
| 1673 |
assert_eq!(drop_counter.get(), 1); |
| 1674 |
} |
| 1675 |
|
| 1676 |
#[test] |
| 1677 |
#[cfg_attr(not(panic = "unwind"), ignore)] |
| 1678 |
fn owned_safe_drop_on_as_ref_panic() { |
| 1679 |
let buf: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 1680 |
let drop_counter = SharedAtomicCounter::new(); |
| 1681 |
let mut owner = OwnedTester::new(buf, drop_counter.clone()); |
| 1682 |
owner.panic_as_ref = true; |
| 1683 |
|
| 1684 |
let result = panic::catch_unwind(AssertUnwindSafe(|| { |
| 1685 |
let _ = Bytes::from_owner(owner); |
| 1686 |
})); |
| 1687 |
|
| 1688 |
assert!(result.is_err()); |
| 1689 |
assert_eq!(drop_counter.get(), 1); |
| 1690 |
} |
| 1691 |
|
| 1692 |
/// Test `BytesMut::put` reuses allocation of `Bytes`. |
| 1693 |
#[test] |
| 1694 |
fn bytes_mut_put_bytes_specialization() { |
| 1695 |
let mut vec = Vec::with_capacity(1234); |
| 1696 |
vec.push(10); |
| 1697 |
let capacity = vec.capacity(); |
| 1698 |
assert!(capacity >= 1234); |
| 1699 |
|
| 1700 |
// Make `Bytes` backed by `Vec`. |
| 1701 |
let bytes = Bytes::from(vec); |
| 1702 |
let mut bytes_mut = BytesMut::new(); |
| 1703 |
bytes_mut.put(bytes); |
| 1704 |
|
| 1705 |
// Check contents is correct. |
| 1706 |
assert_eq!(&[10], bytes_mut.as_ref()); |
| 1707 |
// If allocation is reused, capacity should be equal to original vec capacity. |
| 1708 |
assert_eq!(bytes_mut.capacity(), capacity); |
| 1709 |
} |
| 1710 |
|
| 1711 |
#[test] |
| 1712 |
#[should_panic] |
| 1713 |
fn bytes_mut_reserve_overflow() { |
| 1714 |
let mut a = BytesMut::from(&b"hello world"[..]); |
| 1715 |
let mut b = a.split_off(5); |
| 1716 |
// Ensure b becomes the unique owner of the backing storage |
| 1717 |
drop(a); |
| 1718 |
// Trigger overflow in new_cap + offset inside reserve |
| 1719 |
b.reserve(usize::MAX - 6); |
| 1720 |
// This call relies on the corrupted cap and may cause UB & HBO |
| 1721 |
b.put_u8(b'h'); |
| 1722 |
} |