Skip to content

Commit ef6d4a2

Browse files
committed
Better error message when using the derive on unit structs
1 parent 4f45bfd commit ef6d4a2

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

soa-derive-internal/src/input.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ impl Input {
148148
_ => panic!("#[derive(StructOfArray)] only supports struct"),
149149
};
150150

151+
if fields.is_empty() {
152+
panic!("#[derive(StructOfArray)] only supports struct with fields");
153+
}
154+
151155
let mut extra_attrs = ExtraAttributes::new();
152156

153157
for attr in input.attrs {

src/lib.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
//! }
5252
//! # }
5353
//! ```
54-
//!
55-
//! If you want to add attribute to a specific generated struct(such as
54+
//!
55+
//! If you want to add attribute to a specific generated struct(such as
5656
//! `#[cfg_attr(test, derive(PartialEq))]` on `CheeseVec`), you can add an
57-
//! attribute `#[soa_attr(Vec, cfg_attr(test, derive(PartialEq)))]` to the
57+
//! attribute `#[soa_attr(Vec, cfg_attr(test, derive(PartialEq)))]` to the
5858
//! struct declaration.
59-
//!
59+
//!
6060
//! ```
6161
//! # #[macro_use] extern crate soa_derive;
6262
//! # fn main() {
@@ -70,8 +70,8 @@
7070
//! }
7171
//! # }
7272
//! ```
73-
//!
74-
//! Mappings for first argument of ``soa_attr`` to the generated struct for ``Cheese``:
73+
//!
74+
//! Mappings for first argument of ``soa_attr`` to the generated struct for ``Cheese``:
7575
//! * `Vec` => `CheeseVec`
7676
//! * `Slice` => `CheeseSlice`
7777
//! * `SliceMut` => `CheeseSliceMut`
@@ -188,10 +188,10 @@
188188
// macro_rules macro.
189189
pub use soa_derive_internal::StructOfArray;
190190

191-
/// Any struct derived by StructOfArray will auto impl this trait
192-
/// You can use `<Cheese as StructOfArray>::Type`
193-
/// instead of explicit named type `CheeseVec`; This will helpful in generics programing
194-
/// that generate struct can be expressed as `<T as StructOfArray>::Type`
191+
/// Any struct derived by StructOfArray will auto impl this trait You can use
192+
/// `<Cheese as StructOfArray>::Type` instead of explicit named type
193+
/// `CheeseVec`; This will helpful in generics programing that generate struct
194+
/// can be expressed as `<T as StructOfArray>::Type`
195195
pub trait StructOfArray {
196196
type Type;
197197
}
@@ -221,11 +221,17 @@ pub trait SoAIndex<T>: private_soa_indexes::Sealed {
221221
/// The output for the non-mutable functions
222222
type RefOutput;
223223

224-
/// Returns the reference output in this location if in bounds, `None` otherwise.
224+
/// Returns the reference output in this location if in bounds, `None`
225+
/// otherwise.
225226
fn get(self, soa: T) -> Option<Self::RefOutput>;
226-
/// Returns the reference output in this location without performing any bounds check.
227+
/// Returns the reference output in this location without performing any
228+
/// bounds check.
229+
///
230+
/// # Safety
231+
/// The index must be in bounds.
227232
unsafe fn get_unchecked(self, soa: T) -> Self::RefOutput;
228-
/// Returns the reference output in this location. Panics if it is not in bounds.
233+
/// Returns the reference output in this location. Panics if it is not in
234+
/// bounds.
229235
fn index(self, soa: T) -> Self::RefOutput;
230236
}
231237

@@ -235,11 +241,17 @@ pub trait SoAIndexMut<T>: private_soa_indexes::Sealed {
235241
/// The output for the mutable functions
236242
type MutOutput;
237243

238-
/// Returns the mutable reference output in this location if in bounds, `None` otherwise.
244+
/// Returns the mutable reference output in this location if in bounds,
245+
/// `None` otherwise.
239246
fn get_mut(self, soa: T) -> Option<Self::MutOutput>;
240-
/// Returns the mutable reference output in this location without performing any bounds check.
247+
/// Returns the mutable reference output in this location without performing
248+
/// any bounds check.
249+
///
250+
/// # Safety
251+
/// The index must be in bounds.
241252
unsafe fn get_unchecked_mut(self, soa: T) -> Self::MutOutput;
242-
/// Returns the mutable reference output in this location. Panics if it is not in bounds.
253+
/// Returns the mutable reference output in this location. Panics if it is
254+
/// not in bounds.
243255
fn index_mut(self, soa: T) -> Self::MutOutput;
244256
}
245257

0 commit comments

Comments
 (0)