enum-repr-conv 0.1.0

enum_repr_conv is a proc-macro library that provides a convenient attribute-style `repr_conv()` macro to specify a primitive integer type used for representing variants of the annotated fieldless enum as values of this type.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 27.9 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 180.2 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • vkrivopalov

enum_repr_conv

enum_repr_conv is a proc-macro library that provides a convenient attribute-style repr_conv() macro to specify a primitive integer type used for representing variants of the annotated fieldless enum as values of this type.

Description

enum_repr_conv bridges the gap between C-style enums and fieldless enums in Rust that can be represented using a chosen primitive integer type. It implements conversions from enum variants into integer values (infallible through std::convert::From) and vice versa (fallible through std::convert::TryFrom). This functionality is provided via repr_conv(<type>) macro that mimics the syntax and behavior of repr(u*)/repr(i*) data layout attributes but additionally provides practical and sensible implementations of common conversion traits.

Example

use enum_repr_conv::repr_conv;

#[repr_conv(u16)]
#[derive(Debug, PartialEq, Eq)]
enum SupportedPorts {
	FTP = 21,
	SSH, // = 22
	HTTP = 80,
	HTTPS = 443,
}

// we can get a u16 for the FTP port easily...
assert_eq!(u16::from(SupportedPorts::FTP), 21);
//... and for the SSH port too
assert_eq!(u16::from(SupportedPorts::SSH), 22);
// HTTP port is supported and so conversion succeeds
assert_eq!(SupportedPorts::try_from(80).unwrap(), SupportedPorts::HTTP);
// Gopher is not supported; who needs it in 2025?!
assert!(SupportedPorts::try_from(70).is_err());

License

Licensed under Boost Software License, Version 1.0.