Skip to content

Commit 048b94e

Browse files
authored
no-std feature flag. (#141)
* use core and alloc instead of std This commits uses the `core` and `alloc` crate instead of std as much as possible, without any changes to the code. We are simply importing from a different location. * raw_fd -> c_int This commit changes std::os::unix::io::RawFd into simply c_int, which exists in `core`. * create std feature and adjust imports The worst change here is substituting `last_os_error` to just reading `errno` from `libc`. However, since ALSA can only be used in `linux` systems, which will always have `errno` in their `libc`, with the correct semantics, this should be fine. * add another workflow step to check no-std builds * delete old comment * change RawFd declaration according to std feature * do not need to use core in tests, as std is imported * document that function is not available in no-std * do not import errno in std environments
1 parent 50805d9 commit 048b94e

File tree

19 files changed

+1346
-1254
lines changed

19 files changed

+1346
-1254
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ jobs:
2424
run: sudo apt-get install --no-install-recommends -y libasound2-dev
2525
- name: Build
2626
run: cargo check --verbose --all
27+
- name: Build no-std
28+
run: cargo check --no-default-features --verbose --all
2729
# - name: Run tests
2830
# run: cargo test --verbose --all

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ anyhow = "1.0"
2525
[badges]
2626
is-it-maintained-issue-resolution = { repository = "diwic/alsa-rs" }
2727
is-it-maintained-open-issues = { repository = "diwic/alsa-rs" }
28+
29+
[features]
30+
default = ["std"]
31+
std = []

src/card.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
use libc::{c_int, c_char};
33
use super::error::*;
44
use crate::alsa;
5-
use std::ffi::CStr;
5+
use core::ffi::CStr;
6+
use ::alloc::string::String;
67

78
/// An ALSA sound card, uniquely identified by its index.
89
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
@@ -33,12 +34,12 @@ impl Card {
3334
acheck!(snd_card_get_index(s.as_ptr())).map(Card)
3435
}
3536
pub fn get_name(&self) -> Result<String> {
36-
let mut c: *mut c_char = ::std::ptr::null_mut();
37+
let mut c: *mut c_char = ::core::ptr::null_mut();
3738
acheck!(snd_card_get_name(self.0, &mut c))
3839
.and_then(|_| from_alloc("snd_card_get_name", c))
3940
}
4041
pub fn get_longname(&self) -> Result<String> {
41-
let mut c: *mut c_char = ::std::ptr::null_mut();
42+
let mut c: *mut c_char = ::core::ptr::null_mut();
4243
acheck!(snd_card_get_longname(self.0, &mut c))
4344
.and_then(|_| from_alloc("snd_card_get_longname", c))
4445
}
@@ -48,7 +49,8 @@ impl Card {
4849

4950
#[test]
5051
fn print_cards() {
52+
extern crate std;
5153
for a in Iter::new().map(|a| a.unwrap()) {
52-
println!("Card #{}: {} ({})", a.get_index(), a.get_name().unwrap(), a.get_longname().unwrap())
54+
std::println!("Card #{}: {} ({})", a.get_index(), a.get_name().unwrap(), a.get_longname().unwrap())
5355
}
5456
}

src/chmap.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::alsa;
2-
use std::{fmt, mem, slice};
2+
use core::{fmt, mem, slice};
33
use super::error::*;
4+
use ::alloc::vec::Vec;
5+
use ::alloc::vec;
46

57
alsa_enum!(
68
/// [SND_CHMAP_TYPE_xxx](http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html) constants
@@ -136,8 +138,9 @@ impl Iterator for ChmapsQuery {
136138

137139
#[test]
138140
fn chmap_for_first_pcm() {
141+
extern crate std;
139142
use super::*;
140-
use std::ffi::CString;
143+
use ::alloc::ffi::CString;
141144
use crate::device_name::HintIter;
142145

143146
use crate::Output;
@@ -146,18 +149,18 @@ fn chmap_for_first_pcm() {
146149

147150
let i = HintIter::new(None, &*CString::new("pcm").unwrap()).unwrap();
148151
for p in i.map(|n| n.name.unwrap()) {
149-
println!("Chmaps for {:?}:", p);
152+
std::println!("Chmaps for {:?}:", p);
150153
match PCM::open(&CString::new(p).unwrap(), Direction::Playback, false) {
151154
Ok(a) => for c in a.query_chmaps() {
152-
println!(" {:?}, {}", c.0, c.1);
155+
std::println!(" {:?}, {}", c.0, c.1);
153156
},
154-
Err(a) => println!(" {}", a) // It's okay to have entries in the name hint array that can't be opened
157+
Err(a) => std::println!(" {}", a) // It's okay to have entries in the name hint array that can't be opened
155158
}
156159
}
157160

158161
output.borrow_mut().buffer_string(|buf| {
159162
let str = CString::new(buf).unwrap();
160-
println!("Errors:\n{}", str.to_str().unwrap());
163+
std::println!("Errors:\n{}", str.to_str().unwrap());
161164
});
162165

163166
}

src/config.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::{alsa};
77
use super::error::*;
88
use super::Output;
9-
use std::ptr;
9+
use core::ptr;
1010

1111
pub fn update() -> Result<bool> {
1212
acheck!(snd_config_update()).map(|x| x != 0)
@@ -36,8 +36,9 @@ impl Config {
3636

3737
#[test]
3838
fn config_save() {
39+
extern crate std;
3940
let c = update_ref().unwrap();
4041
let mut outp = Output::buffer_open().unwrap();
4142
c.save(&mut outp).unwrap();
42-
println!("== Config save ==\n{}", outp);
43-
}
43+
std::println!("== Config save ==\n{}", outp);
44+
}

src/ctl_int.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11

22
use crate::alsa;
33
use super::pcm::Info;
4-
use std::ffi::{CStr, CString};
4+
use core::ffi::CStr;
5+
use ::alloc::ffi::CString;
56
use super::Direction;
67
use super::error::*;
78
use super::mixer::MilliBel;
89
use super::Round;
9-
use std::{ptr, mem, fmt, cmp};
10+
use core::{ptr, mem, fmt, cmp};
1011
use crate::{Card, poll};
11-
use std::cell::UnsafeCell;
12+
use core::cell::UnsafeCell;
1213
use libc::{c_uint, c_void, size_t, c_long, c_int, pollfd, c_short};
1314

1415
/// We prefer not to allocate for every ElemId, ElemInfo or ElemValue.
@@ -60,7 +61,7 @@ impl Ctl {
6061
}
6162

6263
pub fn from_card(c: &Card, nonblock: bool) -> Result<Ctl> {
63-
let s = format!("hw:{}", c.get_index());
64+
let s = ::alloc::format!("hw:{}", c.get_index());
6465
Ctl::open(&CString::new(s).unwrap(), nonblock)
6566
}
6667

@@ -294,7 +295,7 @@ impl ElemValue {
294295

295296
pub fn get_bytes(&self) -> Option<&[u8]> {
296297
if self.etype != ElemType::Bytes { None }
297-
else { Some( unsafe { ::std::slice::from_raw_parts(
298+
else { Some( unsafe { ::core::slice::from_raw_parts(
298299
alsa::snd_ctl_elem_value_get_bytes(self.ptr) as *const u8, self.count as usize) } ) }
299300
}
300301

@@ -546,6 +547,7 @@ impl EventMask {
546547

547548
#[test]
548549
fn print_sizeof() {
550+
extern crate std;
549551
let elemid = unsafe { alsa::snd_ctl_elem_id_sizeof() } as usize;
550552
let elemvalue = unsafe { alsa::snd_ctl_elem_value_sizeof() } as usize;
551553
let eleminfo = unsafe { alsa::snd_ctl_elem_info_sizeof() } as usize;
@@ -554,5 +556,5 @@ fn print_sizeof() {
554556
// assert!(elemvalue <= ELEM_VALUE_SIZE);
555557
// assert!(eleminfo <= ELEM_INFO_SIZE);
556558

557-
println!("Elem id: {}, Elem value: {}, Elem info: {}", elemid, elemvalue, eleminfo);
559+
std::println!("Elem id: {}, Elem value: {}, Elem info: {}", elemid, elemvalue, eleminfo);
558560
}

src/device_name.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! Print all devices found in various categories.
55
//!
66
//! ```
7-
//! use std::ffi::CString;
7+
//! use ::alloc::ffi::CString;
88
//! use alsa::device_name::HintIter;
99
//!
1010
//! for t in &["pcm", "ctl", "rawmidi", "timer", "seq", "hwdep"] {
@@ -14,12 +14,14 @@
1414
//! }
1515
//! ```
1616
17-
use std::ptr;
17+
use core::ptr;
1818
use libc::{c_void, c_int};
1919
use crate::alsa;
2020
use super::{Card, Direction};
2121
use super::error::*;
22-
use std::ffi::{CStr, CString};
22+
use core::ffi::CStr;
23+
use ::alloc::ffi::CString;
24+
use ::alloc::string::String;
2325

2426
/// [snd_device_name_hint](http://www.alsa-project.org/alsa-doc/alsa-lib/group___control.html) wrapper
2527
pub struct HintIter(*mut *mut c_void, isize);
@@ -82,9 +84,10 @@ impl Hint {
8284

8385
#[test]
8486
fn print_hints() {
87+
extern crate std;
8588
for t in &["pcm", "ctl", "rawmidi", "timer", "seq", "hwdep"] {
86-
println!("{} devices:", t);
89+
std::println!("{} devices:", t);
8790
let i = HintIter::new(None, &*CString::new(*t).unwrap()).unwrap();
88-
for a in i { println!(" {:?}", a) }
91+
for a in i { std::println!(" {:?}", a) }
8992
}
9093
}

0 commit comments

Comments
 (0)