Skip to content

Commit 5b22ae6

Browse files
committed
Fix no_std support
Make all feature combinations compile. Implement `Debug` and `Hash` everywhere. Expose `as_never` method on `NotSeekable`.
1 parent bf2aaca commit 5b22ae6

9 files changed

+85
-69
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Added a `PitchBend` newtype to manipulate pitch bend values.
2222
- Added a `live` module that allows parsing raw MIDI event bytes.
2323
- Added a `stream` module to support raw MIDI stream decoding.
24+
- All types now implement `Debug`, and all data types implement `Hash`.
2425

2526
## 0.4
2627

src/event.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
/// Represents a parsed SMF track event.
1010
///
1111
/// Consists of a delta time with respect to the previous event and the actual track event.
12-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
12+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
1313
pub struct TrackEvent<'a> {
1414
/// How many MIDI ticks after the previous event should this event fire off.
1515
pub delta: u28,
@@ -59,7 +59,7 @@ impl<'a> TrackEvent<'a> {
5959
///
6060
/// It notably does *not* include the timing of the event; the `TrackEvent` struct is responsible
6161
/// for this.
62-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
62+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
6363
pub enum TrackEventKind<'a> {
6464
/// A message associated to a MIDI channel carrying musical data.
6565
///
@@ -196,7 +196,7 @@ impl<'a> TrackEventKind<'a> {
196196
/// If you wish to parse a MIDI message from a slice of raw MIDI bytes, use the
197197
/// [`LiveEvent::parse`](live/enum.LiveEvent.html#method.parse) method instead and ignore all
198198
/// variants except for [`LiveEvent::Midi`](live/enum.LiveEvent.html#variant.Midi).
199-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
199+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
200200
pub enum MidiMessage {
201201
/// Stop playing a note.
202202
NoteOff {
@@ -352,7 +352,7 @@ impl MidiMessage {
352352
/// A value of `0x0000` indicates full bend downwards.
353353
/// A value of `0x2000` indicates no bend.
354354
/// A value of `0x3FFF` indicates full bend upwards.
355-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
355+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
356356
pub struct PitchBend(pub u14);
357357
impl PitchBend {
358358
/// The minimum value of `0x0000`, indicating full bend downwards.
@@ -405,7 +405,7 @@ impl PitchBend {
405405

406406
/// A "meta message", as defined by the SMF spec.
407407
/// These events carry metadata about the track, such as tempo, time signature, copyright, etc...
408-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
408+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
409409
pub enum MetaMessage<'a> {
410410
/// For `Format::Sequential` MIDI file types, `TrackNumber` can be empty, and defaults to
411411
/// the track index.

src/io.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<W> Clone for NotSeekable<W> {
5858
}
5959
impl<W> Copy for NotSeekable<W> {}
6060
impl<W> NotSeekable<W> {
61-
fn never(self) -> ! {
61+
pub fn as_never(&self) -> ! {
6262
match self.never {}
6363
}
6464
}
@@ -67,7 +67,7 @@ impl<W: Write> Write for NotSeekable<W> {
6767
type Error = W::Error;
6868
type Seekable = Self;
6969
fn write(&mut self, _: &[u8]) -> IoResult<Self> {
70-
self.never()
70+
self.as_never()
7171
}
7272
fn invalid_input(msg: &'static str) -> W::Error {
7373
W::invalid_input(msg)
@@ -76,10 +76,10 @@ impl<W: Write> Write for NotSeekable<W> {
7676

7777
impl<W: Write> Seek for NotSeekable<W> {
7878
fn tell(&mut self) -> StdResult<u64, W::Error> {
79-
self.never()
79+
self.as_never()
8080
}
8181
fn write_at(&mut self, _: &[u8], _: u64) -> IoResult<Self> {
82-
self.never()
82+
self.as_never()
8383
}
8484
}
8585

@@ -129,6 +129,7 @@ impl Seek for Vec<u8> {
129129
/// A seekable writer over an in-memory buffer.
130130
///
131131
/// Available even when the `std` and `alloc` features are disabled.
132+
#[derive(Debug)]
132133
pub struct Cursor<'a> {
133134
buf: &'a mut [u8],
134135
cur: usize,
@@ -239,6 +240,7 @@ impl<'a> Seek for Cursor<'a> {
239240
}
240241

241242
/// The errors that can arise when writing to an in-memory buffer.
243+
#[derive(Debug, Clone)]
242244
pub enum CursorError {
243245
/// The in-memory buffer was too small.
244246
OutOfSpace,
@@ -268,6 +270,7 @@ impl<'a> Write for &'a mut [u8] {
268270
/// Bridge between a `midly::io::Write` type and a `std::io::Write` type.
269271
///
270272
/// Always available, but only implements `midly::io::Write` when the `std` feature is enabled.
273+
#[derive(Debug, Clone, Default)]
271274
pub struct IoWrap<T>(pub T);
272275
#[cfg(feature = "std")]
273276
impl<T: io::Write> Write for IoWrap<T> {
@@ -285,6 +288,7 @@ impl<T: io::Write> Write for IoWrap<T> {
285288
///
286289
/// Always available, but only implements `midly::io::{Write, Seek}` when the `std` feature is
287290
/// enabled.
291+
#[derive(Debug, Clone, Default)]
288292
pub struct SeekableWrap<T>(pub T);
289293
#[cfg(feature = "std")]
290294
impl<T: io::Write + io::Seek> Write for SeekableWrap<T> {

src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,14 @@
108108
//! vel: 127.into(),
109109
//! },
110110
//! };
111+
//! # let mut stack_buf = [0; 3];
111112
//! # let mut buf = {
112113
//! # #[cfg(feature = "alloc")] {
113114
//! let mut buf = Vec::new();
114115
//! # buf
115116
//! # }
116117
//! # #[cfg(not(feature = "alloc"))] {
117-
//! # [0; 3]
118+
//! # &mut stack_buf[..]
118119
//! # }
119120
//! # };
120121
//! ev.write(&mut buf).unwrap();
@@ -183,9 +184,11 @@ mod error;
183184

184185
#[macro_use]
185186
mod prelude {
187+
#[cfg(feature = "std")]
188+
pub(crate) use crate::io::IoWrap;
186189
pub(crate) use crate::{
187190
error::{ErrorKind, Result, ResultExt, StdResult},
188-
io::{IoResult, IoWrap, Seek, Write, WriteCounter},
191+
io::{IoResult, Seek, Write, WriteCounter},
189192
primitive::{u14, u24, u28, u4, u7, IntRead, IntReadBottom7, SplitChecked},
190193
};
191194
#[cfg(feature = "alloc")]

src/live.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{event::MidiMessage, prelude::*};
2020
/// [`TrackEvent`](../struct.TrackEvent.html)s stored in a `.mid` file.
2121
///
2222
/// See the [`live`](index.html) module for more information.
23-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
23+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
2424
pub enum LiveEvent<'a> {
2525
/// A MIDI message associated with a channel, carrying musical data.
2626
Midi { channel: u4, message: MidiMessage },
@@ -94,13 +94,19 @@ impl<'a> LiveEvent<'a> {
9494
}
9595

9696
/// Write a standalone message to the given `std::io::Write` output.
97+
///
98+
/// This method is only available with the `std` feature enabled.
99+
#[cfg(feature = "std")]
97100
pub fn write_std<W: io::Write>(&self, out: W) -> io::Result<()> {
98101
self.write(&mut IoWrap(out))
99102
}
100103

101104
/// Write a message, skipping the status if it shares the status with the previous message.
102105
///
103106
/// Note that it's usually discouraged to feed messages with running status to OS APIs.
107+
///
108+
/// This method is only available with the `std` feature enabled.
109+
#[cfg(feature = "std")]
104110
pub fn write_std_with_running_status<W: io::Write>(
105111
&self,
106112
running_status: &mut Option<u8>,
@@ -113,7 +119,7 @@ impl<'a> LiveEvent<'a> {
113119
/// System Realtime messages are one-byte messages that only occur within live MIDI streams.
114120
/// They are usually time-sensitive, get top priority and can even be transmitted in between other
115121
/// messages.
116-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
122+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
117123
pub enum SystemRealtime {
118124
/// If sent, they should be sent 24 times per quarter note.
119125
TimingClock,
@@ -161,7 +167,7 @@ impl SystemRealtime {
161167
}
162168

163169
/// A "system common event", as defined by the MIDI spec.
164-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
170+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
165171
pub enum SystemCommon<'a> {
166172
/// A system-exclusive event.
167173
///
@@ -245,7 +251,7 @@ impl<'a> SystemCommon<'a> {
245251
}
246252

247253
/// The different kinds of info a Midi Time Code Quarter Frame message can carry.
248-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
254+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
249255
pub enum MtcQuarterFrameMessage {
250256
/// The low nibble of the frame count.
251257
FramesLow,

src/primitive.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ macro_rules! int_feature {
9292
macro_rules! restricted_int {
9393
{$(#[$attr:meta])* $name:ident : $inner:tt => $bits:expr ; $( $feature:tt )* } => {
9494
$(#[$attr])*
95-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
95+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
9696
#[repr(transparent)]
9797
#[allow(non_camel_case_types)]
9898
pub struct $name($inner);
@@ -284,7 +284,7 @@ pub(crate) fn write_varlen_slice<W: Write>(slice: &[u8], out: &mut W) -> IoResul
284284
}
285285

286286
/// The order in which tracks should be laid out when playing back this SMF file.
287-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
287+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
288288
pub enum Format {
289289
/// This file should have a single track only.
290290
///
@@ -323,7 +323,7 @@ impl Format {
323323

324324
/// The timing for an SMF file.
325325
/// This can be in ticks/beat or ticks/second.
326-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
326+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
327327
pub enum Timing {
328328
/// Specifies ticks/beat as a 15-bit integer.
329329
///
@@ -370,7 +370,7 @@ impl Timing {
370370
/// - `second` is inside [0,59]
371371
/// - `frame` is inside [0,fps[
372372
/// - `subframe` is inside [0,99]
373-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
373+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
374374
pub struct SmpteTime {
375375
hour: u8,
376376
minute: u8,
@@ -459,7 +459,7 @@ impl SmpteTime {
459459
}
460460

461461
/// One of the four FPS values available for SMPTE times, as defined by the MIDI standard.
462-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
462+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
463463
pub enum Fps {
464464
/// 24 frames per second.
465465
Fps24,

0 commit comments

Comments
 (0)