Skip to content

Commit

Permalink
fix: make tests check no_std, fix clippy/fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Gankra committed Dec 3, 2023
1 parent 56e230f commit 65c0d80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: MIRIFLAGS=-Zmiri-strict-provenance cargo miri test
- name: Test (gecko-ffi) with Miri
run: MIRIFLAGS=-Zmiri-strict-provenance cargo miri test --features=gecko-ffi

build:
runs-on: ubuntu-latest
steps:
Expand All @@ -39,7 +39,9 @@ jobs:
run: cargo test --features=serde --verbose
- name: Run tests (gecko-ffi)
run: cargo test --tests --features=gecko-ffi --verbose

- name: Run tests (no_std)
run: cargo test --tests --no-default-features --verbose

fmt:
runs-on: ubuntu-latest
steps:
Expand All @@ -54,8 +56,8 @@ jobs:
with:
command: fmt
args: --all -- --check


clippy:
runs-on: ubuntu-latest
steps:
Expand All @@ -72,8 +74,8 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --workspace --tests --examples


docs:
runs-on: ubuntu-latest
env:
Expand Down
30 changes: 17 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,12 @@
//! [pinned]: https://doc.rust-lang.org/std/pin/index.html

#![cfg_attr(not(feature = "std"), no_std)]

#![allow(clippy::comparison_chain, clippy::missing_safety_doc)]

extern crate alloc;

use alloc::{vec::Vec, boxed::Box};
use alloc::alloc::*;
use alloc::{boxed::Box, vec::Vec};
use core::borrow::*;
use core::cmp::*;
use core::convert::TryFrom;
Expand Down Expand Up @@ -861,7 +860,7 @@ impl<T> ThinVec<T> {
/// Note: Because this shifts over the remaining elements, it has a
/// worst-case performance of *O*(*n*). If you don't need the order of elements
/// to be preserved, use [`swap_remove`] instead. If you'd like to remove
/// elements from the beginning of the `ThinVec`, consider using `std::collections::VecDeque`.
/// elements from the beginning of the `ThinVec`, consider using `core::collections::VecDeque`.
///
/// [`swap_remove`]: ThinVec::swap_remove
///
Expand Down Expand Up @@ -1971,7 +1970,7 @@ impl<T> FromIterator<T> for ThinVec<T> {
#[inline]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> ThinVec<T> {
let mut vec = ThinVec::new();
vec.extend(iter.into_iter());
vec.extend(iter);
vec
}
}
Expand Down Expand Up @@ -2043,7 +2042,7 @@ impl<T> From<Box<[T]>> for ThinVec<T> {
}

impl<T> From<Vec<T>> for ThinVec<T> {
/// Convert a `std::Vec` into a `ThinVec`.
/// Convert a `core::Vec` into a `ThinVec`.
///
/// **NOTE:** this must reallocate to change the layout!
///
Expand All @@ -2061,7 +2060,7 @@ impl<T> From<Vec<T>> for ThinVec<T> {
}

impl<T> From<ThinVec<T>> for Vec<T> {
/// Convert a `ThinVec` into a `std::Vec`.
/// Convert a `ThinVec` into a `core::Vec`.
///
/// **NOTE:** this must reallocate to change the layout!
///
Expand Down Expand Up @@ -2640,10 +2639,11 @@ impl std::io::Write for ThinVec<u8> {
#[cfg(test)]
mod tests {
use super::{ThinVec, MAX_CAP};
use crate::alloc::{string::ToString, vec};

#[test]
fn test_size_of() {
use std::mem::size_of;
use core::mem::size_of;
assert_eq!(size_of::<ThinVec<u8>>(), size_of::<&u8>());

assert_eq!(size_of::<Option<ThinVec<u8>>>(), size_of::<&u8>());
Expand Down Expand Up @@ -3016,8 +3016,12 @@ mod std_tests {
#![allow(clippy::reversed_empty_ranges)]

use super::*;
use std::mem::size_of;
use std::usize;
use crate::alloc::{
format,
string::{String, ToString},
};
use core::mem::size_of;
use core::usize;

struct DropCounter<'a> {
count: &'a mut u32,
Expand Down Expand Up @@ -3675,7 +3679,7 @@ mod std_tests {
fn test_splice_forget() {
let mut v = thin_vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
::std::mem::forget(v.splice(2..4, a.iter().cloned()));
::core::mem::forget(v.splice(2..4, a.iter().cloned()));
assert_eq!(v, &[1, 2]);
}

Expand Down Expand Up @@ -4194,16 +4198,16 @@ mod std_tests {
let v: ThinVec<$typename> = ThinVec::with_capacity(1 /* ensure allocation */);
let head_ptr: *mut $typename = v.data_raw();
assert_eq!(
head_ptr as usize % std::mem::align_of::<$typename>(),
head_ptr as usize % core::mem::align_of::<$typename>(),
0,
"expected Header::data<{}> to be aligned",
stringify!($typename)
);
}};
}

const HEADER_SIZE: usize = std::mem::size_of::<Header>();
assert_eq!(2 * std::mem::size_of::<usize>(), HEADER_SIZE);
const HEADER_SIZE: usize = core::mem::size_of::<Header>();
assert_eq!(2 * core::mem::size_of::<usize>(), HEADER_SIZE);

#[repr(C, align(128))]
struct Funky<T>(T);
Expand Down

0 comments on commit 65c0d80

Please sign in to comment.