c2pa-rs

A fork of https://github.com/contentauth/c2pa-rs/
git clone git://archive.git.mtrnord.blog/mtrnords-photography-manager/c2pa-rs.git
Log | Files | Refs | README

boxio.rs (1667B)


      1 // Copyright 2022 Adobe. All rights reserved.
      2 // This file is licensed to you under the Apache License,
      3 // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
      4 // or the MIT license (http://opensource.org/licenses/MIT),
      5 // at your option.
      6 
      7 // Unless required by applicable law or agreed to in writing,
      8 // this software is distributed on an "AS IS" BASIS, WITHOUT
      9 // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
     10 // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
     11 // specific language governing permissions and limitations under
     12 // each license.
     13 
     14 //! This is a library for I/O related constituent elements
     15 //!
     16 //! It is based on the work of Takeru Ohta <phjgt308@gmail.com>
     17 //! and [mse_fmp4](https://github.com/sile/mse_fmp4)
     18 
     19 use std::io::{sink, Result as IoResult, Sink, Write};
     20 
     21 #[derive(Debug)]
     22 pub struct ByteCounter<T> {
     23     inner: T,
     24     count: usize,
     25 }
     26 
     27 impl<T> ByteCounter<T> {
     28     pub const fn new(inner: T) -> Self {
     29         ByteCounter { inner, count: 0 }
     30     }
     31 
     32     pub const fn count(&self) -> usize {
     33         self.count
     34     }
     35 }
     36 
     37 impl ByteCounter<Sink> {
     38     pub fn with_sink() -> Self {
     39         Self::new(sink())
     40     }
     41 
     42     pub fn calculate<F>(f: F) -> IoResult<u64>
     43     where
     44         F: FnOnce(&mut Self) -> IoResult<()>,
     45     {
     46         let mut writer = ByteCounter::with_sink();
     47         f(&mut writer)?;
     48         Ok(writer.count() as u64)
     49     }
     50 }
     51 
     52 impl<T: Write> Write for ByteCounter<T> {
     53     fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
     54         let size = self.inner.write(buf)?;
     55         self.count += size;
     56         Ok(size)
     57     }
     58 
     59     fn flush(&mut self) -> IoResult<()> {
     60         self.inner.flush()
     61     }
     62 }