Skip to main content

RecordingWriter

Struct RecordingWriter 

Source
pub struct RecordingWriter { /* private fields */ }
Expand description

Writes a new plain recording (dataset and metadata) without replacing existing files.

Frames passed to write_frames are encoded into a hidden temporary file next to the final dataset. finish computes the dataset’s SHA-512 digest, stores it in core:sha512, and publishes the dataset and then the metadata. Each file appears under its final name atomically and only if that name is unused, but the pair is not published atomically: if the process is interrupted between the two steps an orphan dataset can remain, but metadata is never published for a dataset that was not. Dropping the writer without calling finish removes the temporary data.

§Examples

use num_complex::Complex;
use sigmf::datatype::Datatype;
use sigmf::global::Global;
use sigmf::meta::Metadata;
use sigmf::recording_file::{Recording, RecordingWriter};
use sigmf::utils::data::SampleValue;
use std::str::FromStr;

let unique = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH)?.as_nanos();
let dir = std::env::temp_dir().join(format!("sigmf-doc-{}-{unique}", std::process::id()));
std::fs::create_dir_all(&dir)?;
let metadata_path = dir.join("example.sigmf-meta");

let mut global = Global::new("1.2.6", Datatype::from_str("cf32_le")?);
global.sample_rate = Some(1e6);
global.num_channels = Some(2);
let metadata = Metadata::new(global, Vec::new(), Vec::new());

// The dataset is written to `example.sigmf-data` in the same directory.
let mut writer = RecordingWriter::new(&metadata_path, metadata)?;
writer.write_frames(&[
    SampleValue::ComplexF32(Complex::new(1.0, 0.0)), // frame 0, channel 0
    SampleValue::ComplexF32(Complex::new(0.0, 1.0)), // frame 0, channel 1
    SampleValue::ComplexF32(Complex::new(-1.0, 0.0)), // frame 1, channel 0
    SampleValue::ComplexF32(Complex::new(0.0, -1.0)), // frame 1, channel 1
])?;
let digest = writer.finish()?;

let mut recording = Recording::open(&metadata_path)?;
assert_eq!(recording.metadata().global().sha512.as_deref(), Some(digest.as_str()));
assert_eq!(recording.dataset_path(), Some(dir.join("example.sigmf-data").as_path()));
recording.verify_dataset_digest()?;
assert_eq!(recording.frame_count()?, 2);
assert_eq!(
    recording.read_frames(1, 1)?,
    [
        SampleValue::ComplexF32(Complex::new(-1.0, 0.0)),
        SampleValue::ComplexF32(Complex::new(0.0, -1.0)),
    ]
);
std::fs::remove_dir_all(&dir)?;

Implementations§

Source§

impl RecordingWriter

Source

pub fn new( metadata_path: impl AsRef<Path>, metadata: Metadata, ) -> Result<Self, SigmfError>

Creates a writer for a new recording whose metadata will be written to metadata_path.

metadata_path must end in .sigmf-meta and its parent directory must exist. The dataset is written next to it, named by core:dataset when set, or otherwise by replacing the .sigmf-meta suffix with .sigmf-data. Neither file may exist yet. core:sha512 is filled in by finish; NCD capture headers and trailing bytes are not synthesized.

§Errors
Source

pub fn write_frames( &mut self, samples: &[SampleValue], ) -> Result<(), SigmfError>

Encodes and appends one block of complete frames.

samples holds one SampleValue per channel per frame, in on-disk order, using the variant that matches core:datatype.

§Errors

Returns an error if the samples do not form complete frames or do not match the datatype (see data::encode), or SigmfError::Io if writing the temporary dataset fails.

Source

pub fn finish(self) -> Result<String, SigmfError>

Flushes and publishes both files, returning the dataset’s SHA-512 digest.

The temporary dataset and metadata files are synced to disk and then published by hard-linking them to their final names, so the destination file system must support hard links. If the metadata cannot be published, the already published dataset is removed again.

§Errors

Trait Implementations§

Source§

impl Drop for RecordingWriter

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more