Skip to content

Commit

Permalink
Add overload for sequences of unmanaged types
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Jan 15, 2025
1 parent 8e029f5 commit 1e31c34
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Bonsai.Dsp/MatrixWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ public unsafe IObservable<TElement[]> Process<TElement>(IObservable<TElement[]>
});
}

/// <summary>
/// Writes all of the values in an observable sequence to the specified raw binary output stream.
/// </summary>
/// <typeparam name="TElement">
/// The type of the elements in the sequence. This type must be a non-pointer, non-nullable
/// unmanaged type.
/// </typeparam>
/// <param name="source">The sequence of values to write.</param>
/// <returns>
/// An observable sequence that is identical to the source sequence but where
/// there is an additional side effect of writing the values to a binary stream.
/// </returns>
public unsafe IObservable<TElement> Process<TElement>(IObservable<TElement> source) where TElement : unmanaged
{
return Process(source, input =>
{
var valuePtr = &input;
var bytes = new byte[sizeof(TElement)];
fixed (byte* bytesPtr = bytes)
System.Buffer.MemoryCopy(valuePtr, bytesPtr, bytes.Length, bytes.Length);
return new ArraySegment<byte>(bytes);
});
}

/// <summary>
/// Writes all of the <see cref="byte"/> arrays in an observable sequence to the
/// specified raw binary output stream.
Expand Down

0 comments on commit 1e31c34

Please sign in to comment.