Skip to content

Commit dd39f2c

Browse files
committed
aud_io: Move alloc utils from lofty
1 parent 6118931 commit dd39f2c

File tree

10 files changed

+40
-32
lines changed

10 files changed

+40
-32
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::error::Result;
2-
use crate::macros::err;
3-
2+
use crate::err;
43
use crate::config::global_options;
54

6-
/// Provides the `fallible_repeat` method on `Vec`
5+
/// Provides the `fallible_repeat` method on [`Vec`]
76
///
8-
/// It is intended to be used in [`try_vec!`](crate::macros::try_vec).
9-
trait VecFallibleRepeat<T>: Sized {
7+
/// It is intended to be used in [`try_vec!`](crate::try_vec).
8+
pub trait VecFallibleRepeat<T>: Sized {
109
fn fallible_repeat(self, element: T, expected_size: usize) -> Result<Self>
1110
where
1211
T: Clone;
@@ -46,23 +45,24 @@ impl<T> VecFallibleRepeat<T> for Vec<T> {
4645

4746
/// **DO NOT USE DIRECTLY**
4847
///
49-
/// Creates a `Vec` of the specified length, containing copies of `element`.
48+
/// Creates a [`Vec`] of the specified length, containing copies of `element`.
5049
///
51-
/// This should be used through [`try_vec!`](crate::macros::try_vec)
52-
pub(crate) fn fallible_vec_from_element<T>(element: T, expected_size: usize) -> Result<Vec<T>>
50+
/// This should be used through [`try_vec!`](crate::try_vec)
51+
#[doc(hidden)]
52+
pub fn fallible_vec_from_element<T>(element: T, expected_size: usize) -> Result<Vec<T>>
5353
where
5454
T: Clone,
5555
{
5656
Vec::new().fallible_repeat(element, expected_size)
5757
}
5858

59-
/// Provides the `try_with_capacity` method on `Vec`
59+
/// Provides the `try_with_capacity` method on [`Vec`]
6060
///
6161
/// This can be used directly.
62-
pub(crate) trait VecFallibleCapacity<T>: Sized {
63-
/// Same as `Vec::with_capacity`, but takes `GlobalOptions::allocation_limit` into account.
62+
pub trait VecFallibleCapacity<T>: Sized {
63+
/// Same as [`Vec::with_capacity()`], but takes [`GlobalOptions::allocation_limit()`] into account.
6464
///
65-
/// Named `try_with_capacity_stable` to avoid conflicts with the nightly `Vec::try_with_capacity`.
65+
/// Named `try_with_capacity_stable` to avoid conflicts with the nightly [`Vec::try_with_capacity()`].
6666
fn try_with_capacity_stable(capacity: usize) -> Result<Self>;
6767
}
6868

@@ -81,7 +81,7 @@ impl<T> VecFallibleCapacity<T> for Vec<T> {
8181

8282
#[cfg(test)]
8383
mod tests {
84-
use crate::util::alloc::fallible_vec_from_element;
84+
use super::fallible_vec_from_element;
8585

8686
#[test_log::test]
8787
fn vec_fallible_repeat() {

aud_io/src/error.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ pub type Result<T> = std::result::Result<T, AudioError>;
55

66
#[derive(Debug)]
77
pub enum AudioError {
8+
// File data related errors
9+
/// Attempting to read/write an abnormally large amount of data
10+
TooMuchData,
11+
812
/// Errors that arise while decoding text
913
TextDecode(&'static str),
1014

@@ -15,6 +19,8 @@ pub enum AudioError {
1519
StringFromUtf8(std::string::FromUtf8Error),
1620
/// Unable to convert bytes to a str
1721
StrFromUtf8(std::str::Utf8Error),
22+
/// Failure to allocate enough memory
23+
Alloc(std::collections::TryReserveError),
1824
/// This should **never** be encountered
1925
Infallible(std::convert::Infallible),
2026
}
@@ -37,6 +43,12 @@ impl From<std::str::Utf8Error> for AudioError {
3743
}
3844
}
3945

46+
impl From<std::collections::TryReserveError> for AudioError {
47+
fn from(input: std::collections::TryReserveError) -> Self {
48+
AudioError::Alloc(input)
49+
}
50+
}
51+
4052
impl From<std::convert::Infallible> for AudioError {
4153
fn from(input: std::convert::Infallible) -> Self {
4254
AudioError::Infallible(input)
@@ -46,12 +58,20 @@ impl From<std::convert::Infallible> for AudioError {
4658
impl Display for AudioError {
4759
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
4860
match self {
61+
AudioError::TextDecode(message) => write!(f, "Text decoding: {message}"),
62+
4963
// Conversions
5064
AudioError::StringFromUtf8(err) => write!(f, "{err}"),
5165
AudioError::StrFromUtf8(err) => write!(f, "{err}"),
5266
AudioError::Io(err) => write!(f, "{err}"),
53-
AudioError::TextDecode(message) => write!(f, "Text decoding: {message}"),
67+
AudioError::Alloc(err) => write!(f, "{err}"),
5468
AudioError::Infallible(_) => write!(f, "An expected condition was not upheld"),
69+
70+
// Files
71+
AudioError::TooMuchData => write!(
72+
f,
73+
"Attempted to read/write an abnormally large amount of data"
74+
),
5575
}
5676
}
5777
}

aud_io/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pub(crate) mod macros;
44
pub mod math;
55
pub mod io;
66
pub mod config;
7+
pub mod alloc;

lofty/src/error.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::file::FileType;
77
use crate::id3::v2::FrameId;
88
use crate::tag::ItemKey;
99

10-
use std::collections::TryReserveError;
1110
use std::fmt::{Debug, Display, Formatter};
1211

1312
use ogg_pager::PageError;
@@ -68,8 +67,6 @@ pub enum ErrorKind {
6867
Io(std::io::Error),
6968
/// Represents all cases of [`std::fmt::Error`].
7069
Fmt(std::fmt::Error),
71-
/// Failure to allocate enough memory
72-
Alloc(TryReserveError),
7370
}
7471

7572
/// The types of errors that can occur while interacting with ID3v2 tags
@@ -493,14 +490,6 @@ impl From<std::fmt::Error> for LoftyError {
493490
}
494491
}
495492

496-
impl From<std::collections::TryReserveError> for LoftyError {
497-
fn from(input: TryReserveError) -> Self {
498-
Self {
499-
kind: ErrorKind::Alloc(input),
500-
}
501-
}
502-
}
503-
504493
impl From<std::convert::Infallible> for LoftyError {
505494
fn from(input: std::convert::Infallible) -> Self {
506495
Self {
@@ -517,7 +506,6 @@ impl Display for LoftyError {
517506
ErrorKind::AudioIo(ref err) => write!(f, "{err}"),
518507
ErrorKind::Io(ref err) => write!(f, "{err}"),
519508
ErrorKind::Fmt(ref err) => write!(f, "{err}"),
520-
ErrorKind::Alloc(ref err) => write!(f, "{err}"),
521509

522510
ErrorKind::UnknownFormat => {
523511
write!(f, "No format could be determined from the provided file")

lofty/src/id3/v2/items/popularimeter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::error::Result;
22
use crate::id3::v2::{FrameFlags, FrameHeader, FrameId};
3-
use crate::util::alloc::VecFallibleCapacity;
43

54
use std::borrow::Cow;
65
use std::hash::{Hash, Hasher};
76
use std::io::Read;
87

8+
use aud_io::alloc::VecFallibleCapacity;
99
use aud_io::text::{TextDecodeOptions, TextEncoding, decode_text, encode_text};
1010
use byteorder::ReadBytesExt;
1111

lofty/src/id3/v2/items/private_frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::error::Result;
22
use crate::id3::v2::{FrameFlags, FrameHeader, FrameId};
3-
use crate::util::alloc::VecFallibleCapacity;
43

54
use std::borrow::Cow;
65
use std::io::Read;
76

7+
use aud_io::alloc::VecFallibleCapacity;
88
use aud_io::text::{TextDecodeOptions, TextEncoding, decode_text, encode_text};
99

1010
const FRAME_ID: FrameId<'static> = FrameId::Valid(Cow::Borrowed("PRIV"));

lofty/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
macro_rules! try_vec {
2-
($elem:expr; $size:expr) => {{ $crate::util::alloc::fallible_vec_from_element($elem, $size)? }};
2+
($elem:expr; $size:expr) => {{ aud_io::alloc::fallible_vec_from_element($elem, $size)? }};
33
}
44

55
// Shorthand for return Err(LoftyError::new(ErrorKind::Foo))

lofty/src/mp4/ilst/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use crate::mp4::ilst::r#ref::AtomRef;
1010
use crate::mp4::read::{AtomReader, atom_tree, find_child_atom, meta_is_full, verify_mp4};
1111
use crate::mp4::write::{AtomWriter, AtomWriterCompanion, ContextualAtom};
1212
use crate::picture::{MimeType, Picture};
13-
use crate::util::alloc::VecFallibleCapacity;
1413

1514
use std::io::{Cursor, Seek, SeekFrom, Write};
1615

1716
use aud_io::io::{FileLike, Length, Truncate};
1817
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
18+
use aud_io::alloc::VecFallibleCapacity;
1919

2020
// A "full" atom is a traditional length + identifier, followed by a version (1) and flags (3)
2121
const FULL_ATOM_SIZE: u64 = ATOM_HEADER_LEN + 4;

lofty/src/mp4/properties.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use crate::config::ParsingMode;
44
use crate::error::{LoftyError, Result};
55
use crate::macros::{decode_err, err, try_vec};
66
use crate::properties::FileProperties;
7-
use crate::util::alloc::VecFallibleCapacity;
87

98
use std::io::{Cursor, Read, Seek, SeekFrom};
109
use std::time::Duration;
1110

11+
use aud_io::alloc::VecFallibleCapacity;
1212
use aud_io::math::RoundedDivision;
1313
use byteorder::{BigEndian, ReadBytesExt};
1414

lofty/src/util/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub(crate) mod alloc;
21
pub(crate) mod io;
32

43
pub(crate) fn flag_item(item: &str) -> Option<bool> {

0 commit comments

Comments
 (0)