Skip to content
Snippets Groups Projects
bytes.rs 12.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    use ByteBuf;
    
    use std::cell::UnsafeCell;
    use std::sync::Arc;
    use std::{cmp, fmt, ops};
    
    /// A reference counted slice of bytes.
    ///
    /// A `Bytes` is an immutable sequence of bytes. Given that it is guaranteed to
    /// be immutable, `Bytes` is `Sync`, `Clone` is shallow (ref count increment),
    /// and all operations only update views into the underlying data without
    /// requiring any copies.
    #[derive(Eq)]
    pub struct Bytes {
        inner: BytesMut,
    }
    
    /// A unique reference to a slice of bytes.
    ///
    /// A `BytesMut` is a unique handle to a slice of bytes allowing mutation of
    /// the underlying bytes.
    pub struct BytesMut {
        mem: Mem,
        pos: usize,
        len: usize,
        cap: usize,
    }
    
    struct Mem {
        inner: Arc<UnsafeCell<Box<[u8]>>>,
    }
    
    /*
     *
     * ===== Bytes =====
     *
     */
    
    impl Bytes {
        /// Creates a new `Bytes` and copy the given slice into it.
        pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> Bytes {
            BytesMut::from_slice(bytes).freeze()
        }
    
        /// Returns the number of bytes contained in this `Bytes`.
        pub fn len(&self) -> usize {
            self.inner.len()
        }
    
        /// Returns true if the value contains no bytes
        pub fn is_empty(&self) -> bool {
            self.inner.is_empty()
        }
    
        /// Returns the inner contents of this `Bytes` as a slice.
        pub fn as_slice(&self) -> &[u8] {
            self.as_ref()
        }
    
        /// Extracts a new `Bytes` referencing the bytes from range [start, end).
        pub fn slice(&self, start: usize, end: usize) -> Bytes {
            let mut ret = self.clone();
    
            ret.inner
                .set_end(end)
                .set_start(start);
    
            ret
        }
    
        /// Extracts a new `Bytes` referencing the bytes from range [start, len).
        pub fn slice_from(&self, start: usize) -> Bytes {
            self.slice(start, self.len())
        }
    
        /// Extracts a new `Bytes` referencing the bytes from range [0, end).
        pub fn slice_to(&self, end: usize) -> Bytes {
            self.slice(0, end)
        }
    
        /// Splits the bytes into two at the given index.
        ///
        /// Afterwards `self` contains elements `[0, at)`, and the returned `Bytes`
        /// contains elements `[at, len)`.
        ///
        /// This is an O(1) operation that just increases the reference count and
        /// sets a few indexes.
        ///
        /// # Panics
        ///
        /// Panics if `at > len`
        pub fn split_off(&mut self, at: usize) -> Bytes {
            self.inner.split_off(at).freeze()
        }
    
        /// Splits the buffer into two at the given index.
        ///
        /// Afterwards `self` contains elements `[at, len)`, and the returned
        /// `Bytes` contains elements `[0, at)`.
        ///
        /// This is an O(1) operation that just increases the reference count and
        /// sets a few indexes.
        ///
        /// # Panics
        ///
        /// Panics if `at > len`
        pub fn drain_to(&mut self, at: usize) -> Bytes {
            self.inner.drain_to(at).freeze()
        }
    
        /// Attempt to convert into a `BytesMut` handle.
        ///
        /// This will only succeed if there are no other outstanding references to
        /// the underlying chunk of memory.
        pub fn try_mut(mut self) -> Result<BytesMut, Bytes> {
            if self.inner.mem.is_mut_safe() {
                Ok(self.inner)
            } else {
                Err(self)
            }
        }
    
        /// Consumes handle, returning a new mutable handle
        ///
        /// The function attempts to avoid copying, however if it is unable to
        /// obtain a unique reference to the underlying data, a new buffer is
        /// allocated and the data is copied to it.
        pub fn into_mut(self) -> BytesMut {
            self.try_mut().unwrap_or_else(BytesMut::from_slice)
        }
    }
    
    impl Clone for Bytes {
        fn clone(&self) -> Bytes {
            Bytes { inner: self.inner.clone() }
        }
    }
    
    impl AsRef<[u8]> for Bytes {
        fn as_ref(&self) -> &[u8] {
            self.inner.as_ref()
        }
    }
    
    impl ops::Deref for Bytes {
        type Target = [u8];
    
        fn deref(&self) -> &[u8] {
            self.as_ref()
        }
    }
    
    impl From<Vec<u8>> for Bytes {
        fn from(src: Vec<u8>) -> Bytes {
            BytesMut::from(src).freeze()
        }
    }
    
    impl<'a> From<&'a [u8]> for Bytes {
        fn from(src: &'a [u8]) -> Bytes {
            BytesMut::from(src).freeze()
        }
    }
    
    impl PartialEq for Bytes {
        fn eq(&self, other: &Bytes) -> bool {
            self.inner == other.inner
        }
    }
    
    impl fmt::Debug for Bytes {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            fmt::Debug::fmt(&self.inner, fmt)
        }
    }
    
    unsafe impl Sync for Bytes {}
    
    /*
     *
     * ===== BytesMut =====
     *
     */
    
    impl BytesMut {
        /// Create a new `BytesMut` with the specified capacity.
        pub fn with_capacity(cap: usize) -> BytesMut {
            BytesMut {
                mem: Mem::with_capacity(cap),
                pos: 0,
                len: 0,
                cap: cap,
            }
        }
    
        /// Creates a new `BytesMut` and copy the given slice into it.
        pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> BytesMut {
            let buf = ByteBuf::from_slice(bytes);
            buf.into_inner()
        }
    
        /// Returns the number of bytes contained in this `BytesMut`.
        pub fn len(&self) -> usize {
            self.len
        }
    
        /// Returns true if the value contains no bytes
        pub fn is_empty(&self) -> bool {
            self.len() == 0
        }
    
        /// Returns the total byte capacity of this `BytesMut`
        pub fn capacity(&self) -> usize {
            self.cap
        }
    
        /// Return an immutable handle to the bytes
        pub fn freeze(self) -> Bytes {
            Bytes { inner: self }
        }
    
        /// Splits the bytes into two at the given index.
        ///
        /// Afterwards `self` contains elements `[0, at)`, and the returned
        /// `BytesMut` contains elements `[at, capacity)`.
        ///
        /// This is an O(1) operation that just increases the reference count and
        /// sets a few indexes.
        ///
        /// # Panics
        ///
        /// Panics if `at > capacity`
        pub fn split_off(&mut self, at: usize) -> BytesMut {
            let mut other = self.clone();
    
            other.set_start(at);
            self.set_end(at);
    
            return other
        }
    
        /// Splits the buffer into two at the given index.
        ///
        /// Afterwards `self` contains elements `[at, len)`, and the returned `BytesMut`
        /// contains elements `[0, at)`.
        ///
        /// This is an O(1) operation that just increases the reference count and
        /// sets a few indexes.
        ///
        /// # Panics
        ///
        /// Panics if `at > len`
        pub fn drain_to(&mut self, at: usize) -> BytesMut {
            let mut other = self.clone();
    
            other.set_end(at);
            self.set_start(at);
    
            return other
        }
    
        /// Returns the inner contents of this `BytesMut` as a slice.
        pub fn as_slice(&self) -> &[u8] {
            self.as_ref()
        }
    
        /// Returns the inner contents of this `BytesMut` as a mutable slice
        ///
        /// This a slice of bytes that have been initialized
        pub fn as_mut(&mut self) -> &mut [u8] {
            let end = self.pos + self.len;
            &mut self.mem.as_mut()[self.pos..end]
        }
    
        /// Sets the length of the buffer
        ///
        /// This will explicitly set the size of the buffer without actually
        /// modifying the data, so it is up to the caller to ensure that the data
        /// has been initialized.
        ///
        /// # Panics
        ///
        /// This method will panic if `len` is out of bounds for the underlying
        /// slice or if it comes after the `end` of the configured window.
        pub unsafe fn set_len(&mut self, len: usize) {
            assert!(len <= self.cap);
            self.len = len;
        }
    
        /// Returns the inner contents of this `BytesMut` as a mutable slice
        ///
        /// This a slice of all bytes, including uninitialized memory
        pub unsafe fn as_raw(&mut self) -> &mut [u8] {
            let end = self.pos + self.cap;
            &mut self.mem.as_mut()[self.pos..end]
        }
    
        /// Changes the starting index of this window to the index specified.
        ///
        /// Returns the windows back to chain multiple calls to this method.
        ///
        /// # Panics
        ///
        /// This method will panic if `start` is out of bounds for the underlying
        /// slice.
        fn set_start(&mut self, start: usize) -> &mut BytesMut {
            assert!(start <= self.cap);
            self.pos += start;
    
            if self.len >= start {
                self.len -= start;
            } else {
                self.len = 0;
            }
    
            self.cap -= start;
            self
        }
    
        /// Changes the end index of this window to the index specified.
        ///
        /// Returns the windows back to chain multiple calls to this method.
        ///
        /// # Panics
        ///
        /// This method will panic if `start` is out of bounds for the underlying
        /// slice.
        fn set_end(&mut self, end: usize) -> &mut BytesMut {
            assert!(end <= self.cap);
            self.cap = end;
            self.len = cmp::min(self.len, end);
            self
        }
    
        /// Increments the ref count. This should only be done if it is known that
        /// it can be done safely. As such, this fn is not public, instead other
        /// fns will use this one while maintaining the guarantees.
        fn clone(&self) -> BytesMut {
             BytesMut {
                mem: self.mem.clone(),
                .. *self
            }
        }
    }
    
    impl AsRef<[u8]> for BytesMut {
        fn as_ref(&self) -> &[u8] {
            let end = self.pos + self.len;
            &self.mem.as_ref()[self.pos..end]
        }
    }
    
    impl ops::Deref for BytesMut {
        type Target = [u8];
    
        fn deref(&self) -> &[u8] {
            self.as_ref()
        }
    }
    
    impl ops::DerefMut for BytesMut {
        fn deref_mut(&mut self) -> &mut [u8] {
            self.as_mut()
        }
    }
    
    impl From<Vec<u8>> for BytesMut {
        fn from(src: Vec<u8>) -> BytesMut {
            let len = src.len();
            let cap = src.capacity();
    
            BytesMut {
                mem: Mem::from_vec(src),
                pos: 0,
                len: len,
                cap: cap,
            }
        }
    }
    
    impl<'a> From<&'a [u8]> for BytesMut {
        fn from(src: &'a [u8]) -> BytesMut {
            BytesMut::from_slice(src)
        }
    }
    
    impl PartialEq for BytesMut {
        fn eq(&self, other: &BytesMut) -> bool {
            **self == **other
        }
    }
    
    impl Eq for BytesMut {
    }
    
    impl fmt::Debug for BytesMut {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            fmt::Debug::fmt(self.as_ref(), fmt)
        }
    }
    
    unsafe impl Send for BytesMut {}
    
    /*
     *
     * ===== Mem =====
     *
     */
    
    impl Mem {
        fn with_capacity(cap: usize) -> Mem {
            let mut vec = Vec::with_capacity(cap);
            unsafe { vec.set_len(cap); }
    
            Mem { inner: Arc::new(UnsafeCell::new(vec.into_boxed_slice())) }
        }
    
        fn from_vec(mut vec: Vec<u8>) -> Mem {
            let cap = vec.capacity();
            unsafe { vec.set_len(cap); }
    
            Mem { inner: Arc::new(UnsafeCell::new(vec.into_boxed_slice())) }
        }
    
        fn as_ref(&self) -> &[u8] {
            unsafe { &*self.inner.get() }
        }
    
        fn as_mut(&mut self) -> &mut [u8] {
            unsafe { &mut *self.inner.get() }
        }
    
        fn is_mut_safe(&mut self) -> bool {
            Arc::get_mut(&mut self.inner).is_some()
        }
    
        fn clone(&self) -> Mem {
            Mem { inner: self.inner.clone() }
        }
    }
    
    /*
     *
     * ===== PartialEq =====
     *
     */
    
    impl PartialEq<[u8]> for BytesMut {
        fn eq(&self, other: &[u8]) -> bool {
            &**self == other
        }
    }
    
    impl PartialEq<BytesMut> for [u8] {
        fn eq(&self, other: &BytesMut) -> bool {
            *other == *self
        }
    }
    
    impl PartialEq<Vec<u8>> for BytesMut {
        fn eq(&self, other: &Vec<u8>) -> bool {
            *self == &other[..]
        }
    }
    
    impl PartialEq<BytesMut> for Vec<u8> {
        fn eq(&self, other: &BytesMut) -> bool {
            *other == *self
        }
    }
    
    impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut
        where BytesMut: PartialEq<T>
    {
        fn eq(&self, other: &&'a T) -> bool {
            *self == **other
        }
    }
    
    impl<'a> PartialEq<BytesMut> for &'a [u8] {
        fn eq(&self, other: &BytesMut) -> bool {
            *other == *self
        }
    }
    
    impl PartialEq<[u8]> for Bytes {
        fn eq(&self, other: &[u8]) -> bool {
            self.inner == *other
        }
    }
    
    impl PartialEq<Bytes> for [u8] {
        fn eq(&self, other: &Bytes) -> bool {
            *other == *self
        }
    }
    
    impl PartialEq<Vec<u8>> for Bytes {
        fn eq(&self, other: &Vec<u8>) -> bool {
            *self == &other[..]
        }
    }
    
    impl PartialEq<Bytes> for Vec<u8> {
        fn eq(&self, other: &Bytes) -> bool {
            *other == *self
        }
    }
    
    impl<'a> PartialEq<Bytes> for &'a [u8] {
        fn eq(&self, other: &Bytes) -> bool {
            *other == *self
        }
    }
    
    impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes
        where Bytes: PartialEq<T>
    {
        fn eq(&self, other: &&'a T) -> bool {
            *self == **other
        }
    }