cbor/
skip.rs

1// This Source Code Form is subject to the terms of
2// the Mozilla Public License, v. 2.0. If a copy of
3// the MPL was not distributed with this file, You
4// can obtain one at http://mozilla.org/MPL/2.0/.
5
6//! `Skip` trait to allow efficient skipping of consecutive bytes.
7
8use std::i64;
9use std::io::{Result, Error, ErrorKind, Seek, SeekFrom};
10
11/// Type which supports skipping a number of bytes.
12///
13/// Similar in spirit to `std::io::Seek` but only allows
14/// uni-directional movement.
15pub trait Skip {
16    /// Skip over `n` consecutive bytes.
17    fn skip(&mut self, n: u64) -> Result<()>;
18}
19
20impl<A: Seek> Skip for A {
21    /// `n` must be in range `[0, i64::MAX]`.
22    fn skip(&mut self, n: u64) -> Result<()> {
23        if n > i64::MAX as u64 {
24            return Err(Error::new(ErrorKind::Other, "n too large"))
25        }
26        self.seek(SeekFrom::Current(n as i64)).and(Ok(()))
27    }
28}