|
284 | 284 | #![no_std]
|
285 | 285 | #![deny(unsafe_code)]
|
286 | 286 | #![deny(missing_docs)]
|
287 |
| -// Enable pedantic lints as warnings so we don't break builds when |
288 |
| -// lints are modified or new lints are added to clippy. |
289 |
| -#![warn( |
290 |
| - // Enable more useful rustc lints |
291 |
| - absolute_paths_not_starting_with_crate, |
292 |
| - elided_lifetimes_in_paths, |
293 |
| - explicit_outlives_requirements, |
294 |
| - keyword_idents, |
295 |
| - macro_use_extern_crate, |
296 |
| - meta_variable_misuse, |
297 |
| - missing_copy_implementations, |
298 |
| - non_ascii_idents, |
299 |
| - noop_method_call, |
300 |
| - pointer_structural_match, |
301 |
| - single_use_lifetimes, |
302 |
| - trivial_casts, |
303 |
| - trivial_numeric_casts, |
304 |
| - unreachable_pub, |
305 |
| - unused_extern_crates, |
306 |
| - unused_import_braces, |
307 |
| - unused_lifetimes, |
308 |
| - unused_macro_rules, |
309 |
| - unused_qualifications, |
310 |
| - unused_tuple_struct_fields, |
311 |
| - variant_size_differences, |
312 |
| - // Enable pedantic clippy lints |
313 |
| - clippy::pedantic, |
314 |
| - // Useful clippy lints for no_std support |
315 |
| - clippy::std_instead_of_core, |
316 |
| - clippy::std_instead_of_alloc, |
317 |
| - clippy::alloc_instead_of_core, |
318 |
| - // Useful extra perf lints |
319 |
| - clippy::missing_inline_in_public_items |
320 |
| -)] |
321 |
| -// `clippy::pedantic` exceptions |
322 |
| -#![allow( |
323 |
| - // style choice |
324 |
| - clippy::module_name_repetitions, |
325 |
| - // false positive |
326 |
| - clippy::type_repetition_in_bounds, |
327 |
| - // false positive |
328 |
| - clippy::expl_impl_clone_on_copy |
329 |
| -)] |
| 287 | +#![warn(absolute_paths_not_starting_with_crate)] |
| 288 | +#![warn(elided_lifetimes_in_paths)] |
| 289 | +#![warn(explicit_outlives_requirements)] |
| 290 | +#![warn(keyword_idents)] |
| 291 | +#![warn(macro_use_extern_crate)] |
| 292 | +#![warn(meta_variable_misuse)] |
| 293 | +#![warn(missing_copy_implementations)] |
| 294 | +#![warn(non_ascii_idents)] |
| 295 | +#![warn(noop_method_call)] |
| 296 | +#![warn(pointer_structural_match)] |
| 297 | +#![warn(single_use_lifetimes)] |
| 298 | +#![warn(trivial_casts)] |
| 299 | +#![warn(trivial_numeric_casts)] |
| 300 | +#![warn(unreachable_pub)] |
| 301 | +#![warn(unused_extern_crates)] |
| 302 | +#![warn(unused_import_braces)] |
| 303 | +#![warn(unused_lifetimes)] |
| 304 | +#![warn(unused_macro_rules)] |
| 305 | +#![warn(unused_qualifications)] |
| 306 | +#![warn(unused_tuple_struct_fields)] |
| 307 | +#![warn(variant_size_differences)] |
| 308 | +#![warn(clippy::pedantic)] |
| 309 | +#![warn(clippy::std_instead_of_core)] |
| 310 | +#![warn(clippy::std_instead_of_alloc)] |
| 311 | +#![warn(clippy::alloc_instead_of_core)] |
| 312 | +#![warn(clippy::missing_inline_in_public_items)] |
| 313 | +#![allow(clippy::module_name_repetitions)] |
| 314 | +#![allow(clippy::type_repetition_in_bounds)] |
| 315 | +#![allow(clippy::expl_impl_clone_on_copy)] |
330 | 316 |
|
331 | 317 | mod key;
|
332 | 318 | pub use self::key::Key;
|
| 319 | + |
| 320 | +/// Derive to implement the [`Key`] trait. |
| 321 | +/// |
| 322 | +/// This derive implements the [`Key`] trait for a given type. |
| 323 | +/// |
| 324 | +/// The [`Key`] trait is what allows `fixed_map` to set up storage for a type |
| 325 | +/// that will be the key in a fixed map. |
| 326 | +/// |
| 327 | +/// > Note: this requires the `::fixed_map` crate to be in scope when it's |
| 328 | +/// > derived. |
| 329 | +/// |
| 330 | +/// # Container attributes |
| 331 | +/// |
| 332 | +/// #### `#[key(bitset)]` |
| 333 | +/// |
| 334 | +/// This ensures that backing storage is performed with a bitset when used with |
| 335 | +/// a [`Set`]. |
| 336 | +/// |
| 337 | +/// ```rust |
| 338 | +/// use fixed_map::{Key, Set}; |
| 339 | +/// |
| 340 | +/// #[derive(Clone, Copy, Key)] |
| 341 | +/// pub enum Regular { |
| 342 | +/// First, |
| 343 | +/// Second, |
| 344 | +/// Third, |
| 345 | +/// } |
| 346 | +/// |
| 347 | +/// #[derive(Clone, Copy, Key)] |
| 348 | +/// #[key(bitset)] |
| 349 | +/// pub enum Bits { |
| 350 | +/// First, |
| 351 | +/// Second, |
| 352 | +/// Third, |
| 353 | +/// } |
| 354 | +/// |
| 355 | +/// // Normal storage uses an array of booleans: |
| 356 | +/// assert_eq!(std::mem::size_of::<Set<Regular>>(), 3); |
| 357 | +/// |
| 358 | +/// // Bitset storage uses a single u8 (or other appropriate type based on size): |
| 359 | +/// assert_eq!(std::mem::size_of::<Set<Bits>>(), 1); |
| 360 | +/// ``` |
| 361 | +/// |
| 362 | +/// > **Warning:** not all operations will be implemented when this attributes |
| 363 | +/// > is present, so some container methods might not work. |
| 364 | +/// |
| 365 | +/// # Guide |
| 366 | +/// |
| 367 | +/// Given the following enum: |
| 368 | +/// |
| 369 | +/// ```rust |
| 370 | +/// use fixed_map::Key; |
| 371 | +/// |
| 372 | +/// #[derive(Clone, Copy, Key)] |
| 373 | +/// pub enum MyKey { |
| 374 | +/// First, |
| 375 | +/// Second, |
| 376 | +/// Third, |
| 377 | +/// } |
| 378 | +/// ``` |
| 379 | +/// |
| 380 | +/// It performs the following expansion: |
| 381 | +/// |
| 382 | +/// ```rust,no_compile,no_run |
| 383 | +/// use fixed_map::Key; |
| 384 | +/// |
| 385 | +/// #[derive(Clone, Copy)] |
| 386 | +/// pub enum MyKey { |
| 387 | +/// First, |
| 388 | +/// Second, |
| 389 | +/// Third, |
| 390 | +/// } |
| 391 | +/// |
| 392 | +/// /// Build a storage struct containing an item for each key: |
| 393 | +/// pub struct MyKeyMapStorage<V> { |
| 394 | +/// /// Storage for `MyKey::First`. |
| 395 | +/// f1: Option<V>, |
| 396 | +/// /// Storage for `MyKey::Second`. |
| 397 | +/// f2: Option<V>, |
| 398 | +/// /// Storage for `MyKey::Third`. |
| 399 | +/// f3: Option<V>, |
| 400 | +/// } |
| 401 | +/// |
| 402 | +/// /// Build a storage struct containing an element for each key: |
| 403 | +/// pub struct MyKeySetStorage { |
| 404 | +/// data: [bool; 3], |
| 405 | +/// } |
| 406 | +/// |
| 407 | +/// /// Implement map storage for key. |
| 408 | +/// impl<V> fixed_map::map::storage::MapStorage<MyKey, V> for MyKeyMapStorage<V> { |
| 409 | +/// fn get(&self, key: MyKey) -> Option<&V> { |
| 410 | +/// match key { |
| 411 | +/// MyKey::First => self.f1.as_ref(), |
| 412 | +/// MyKey::Second => self.f2.as_ref(), |
| 413 | +/// MyKey::Third => self.f3.as_ref(), |
| 414 | +/// } |
| 415 | +/// } |
| 416 | +/// |
| 417 | +/// /* skipped */ |
| 418 | +/// } |
| 419 | +/// |
| 420 | +/// /// Implement set storage for key. |
| 421 | +/// impl fixed_map::set::storage::SetStorage<MyKey> for MyKeySetStorage { |
| 422 | +/// fn contains(&self, key: MyKey) -> Option<&V> { |
| 423 | +/// let [a, b, c] = &self.data; |
| 424 | +/// |
| 425 | +/// match key { |
| 426 | +/// MyKey::First => a, |
| 427 | +/// MyKey::Second => b, |
| 428 | +/// MyKey::Third => c, |
| 429 | +/// } |
| 430 | +/// } |
| 431 | +/// |
| 432 | +/// /* skipped */ |
| 433 | +/// } |
| 434 | +/// |
| 435 | +/// impl Default for MyKeyMapStorage<V> { |
| 436 | +/// fn default() -> Self { |
| 437 | +/// Self { |
| 438 | +/// f1: None, |
| 439 | +/// f2: None, |
| 440 | +/// f3: None, |
| 441 | +/// } |
| 442 | +/// } |
| 443 | +/// } |
| 444 | +/// |
| 445 | +/// impl Default for MyKeySetStorage { |
| 446 | +/// fn default() -> Self { |
| 447 | +/// Self { |
| 448 | +/// data: [false, false, false] |
| 449 | +/// } |
| 450 | +/// } |
| 451 | +/// } |
| 452 | +/// |
| 453 | +/// /// Implement the Key trait to point out storage. |
| 454 | +/// impl fixed_map::Key for MyKey { |
| 455 | +/// type MapStorage<V> = MyKeyMapStorage<V>; |
| 456 | +/// type SetStorage = MyKeySetStorage; |
| 457 | +/// } |
| 458 | +/// ``` |
333 | 459 | #[doc(inline)]
|
334 |
| -pub use fixed_map_derive::*; |
| 460 | +pub use fixed_map_derive::Key; |
335 | 461 |
|
336 | 462 | pub mod map;
|
337 | 463 | #[doc(inline)]
|
|
0 commit comments