Skip to main content

OnePass

Struct OnePass 

Source
pub struct OnePass<S: PatchSink> { /* private fields */ }
Expand description

Writes a file as its messages arrive, in one pass: the header goes first with no length, and OnePass::finish patches the length in. For sources that cannot run twice, such as a live recording or a conversion read once; the bytes are those write() would write.

use fit_encoder::*;

let start = Timestamp::from_unix(1_764_547_200).unwrap();
let mut buf = [0u8; 2048];
let mut file = OnePass::new(SliceSink::new(&mut buf), Options::new()).unwrap();
let mut w = file.writer();
w.start(start, &Device::new(Manufacturer::DEVELOPMENT, 1)).unwrap();
w.session(start, Sport::WALKING, SubSport::GENERIC).unwrap();
for s in 0..10 {
    w.record(&Record::new(start.checked_add(s).unwrap()).heart_rate(100)).unwrap();
}
w.end(start.checked_add(10).unwrap()).unwrap();
let sink = file.finish().unwrap();
assert_eq!(&sink.written()[8..12], b".FIT");

Implementations§

Source§

impl<S: PatchSink> OnePass<S>

Source

pub fn new(sink: S, options: Options) -> Result<Self, S::Error>

Writes the header, its length to come.

§Errors

The sink’s.

Source

pub fn writer(&mut self) -> Writer<'_>

The writer for the next messages.

Source

pub fn finish(self) -> Result<S, FinishError<Self, S::Error>>

Patches the header’s length in and writes the file CRC. On failure the file comes back with the error, so an activity not yet ended can be ended and finished again; after a sink error, finishing fails again.

§Errors

WriteError::Sink with the sink’s error, including one a message met, or WriteError::Source with Error::Unfinished if the activity was not ended.

Source

pub fn into_sink(self) -> S

The sink, giving up on the file: its header still claims no messages, so readers refuse it.

Trait Implementations§

Source§

impl<S: PatchSink> Debug for OnePass<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Forget for OnePass<S>

§

impl<S> Freeze for OnePass<S>
where S: Freeze, <S as Sink>::Error: Freeze, &'static [FieldDef]: Freeze,

§

impl<S> Move for OnePass<S>

§

impl<S> RefUnwindSafe for OnePass<S>

§

impl<S> Send for OnePass<S>
where S: Send, <S as Sink>::Error: Send, &'static [FieldDef]: Send,

§

impl<S> Sync for OnePass<S>
where S: Sync, <S as Sink>::Error: Sync,

§

impl<S> Unpin for OnePass<S>
where S: Unpin, <S as Sink>::Error: Unpin, &'static [FieldDef]: Unpin,

§

impl<S> UnsafeUnpin for OnePass<S>
where S: UnsafeUnpin, <S as Sink>::Error: UnsafeUnpin, &'static [FieldDef]: UnsafeUnpin,

§

impl<S> UnwindSafe for OnePass<S>
where S: UnwindSafe, <S as Sink>::Error: UnwindSafe, &'static [FieldDef]: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.