Skip to content

Commit c89a430

Browse files
committed
Clean up bytes examples
1 parent e402e20 commit c89a430

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

src/ops/bytes.rs

+8-28
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,8 @@ pub trait ToBytes {
4545
/// ```
4646
/// use num_traits::ToBytes;
4747
///
48-
/// # #[cfg(has_int_to_from_bytes)]
49-
/// # fn main() {
50-
/// let bytes = 0x12345678u32.to_be_bytes();
48+
/// let bytes = ToBytes::to_be_bytes(&0x12345678u32);
5149
/// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]);
52-
/// # }
53-
/// # #[cfg(not(has_int_to_from_bytes))]
54-
/// # fn main() {}
5550
/// ```
5651
fn to_be_bytes(&self) -> Self::Bytes;
5752

@@ -62,13 +57,8 @@ pub trait ToBytes {
6257
/// ```
6358
/// use num_traits::ToBytes;
6459
///
65-
/// # #[cfg(has_int_to_from_bytes)]
66-
/// # fn main() {
67-
/// let bytes = 0x12345678u32.to_le_bytes();
60+
/// let bytes = ToBytes::to_le_bytes(&0x12345678u32);
6861
/// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);
69-
/// # }
70-
/// # #[cfg(not(has_int_to_from_bytes))]
71-
/// # fn main() {}
7262
/// ```
7363
fn to_le_bytes(&self) -> Self::Bytes;
7464

@@ -85,19 +75,14 @@ pub trait ToBytes {
8575
/// ```
8676
/// use num_traits::ToBytes;
8777
///
88-
/// # #[cfg(has_int_to_from_bytes)]
89-
/// # fn main() {
9078
/// #[cfg(target_endian = "big")]
9179
/// let expected = [0x12, 0x34, 0x56, 0x78];
9280
///
93-
/// #[cfg(not(target_endian = "big"))]
81+
/// #[cfg(target_endian = "little")]
9482
/// let expected = [0x78, 0x56, 0x34, 0x12];
9583
///
96-
/// let bytes = 0x12345678u32.to_ne_bytes();
84+
/// let bytes = ToBytes::to_ne_bytes(&0x12345678u32);
9785
/// assert_eq!(bytes, expected)
98-
/// # }
99-
/// # #[cfg(not(has_int_to_from_bytes))]
100-
/// # fn main() {}
10186
/// ```
10287
fn to_ne_bytes(&self) -> Self::Bytes {
10388
#[cfg(target_endian = "big")]
@@ -118,7 +103,7 @@ pub trait FromBytes: Sized {
118103
/// ```
119104
/// use num_traits::FromBytes;
120105
///
121-
/// let value = <u32 as FromBytes>::from_be_bytes(&[0x12, 0x34, 0x56, 0x78]);
106+
/// let value: u32 = FromBytes::from_be_bytes(&[0x12, 0x34, 0x56, 0x78]);
122107
/// assert_eq!(value, 0x12345678);
123108
/// ```
124109
fn from_be_bytes(bytes: &Self::Bytes) -> Self;
@@ -130,7 +115,7 @@ pub trait FromBytes: Sized {
130115
/// ```
131116
/// use num_traits::FromBytes;
132117
///
133-
/// let value = <u32 as FromBytes>::from_le_bytes(&[0x78, 0x56, 0x34, 0x12]);
118+
/// let value: u32 = FromBytes::from_le_bytes(&[0x78, 0x56, 0x34, 0x12]);
134119
/// assert_eq!(value, 0x12345678);
135120
/// ```
136121
fn from_le_bytes(bytes: &Self::Bytes) -> Self;
@@ -148,19 +133,14 @@ pub trait FromBytes: Sized {
148133
/// ```
149134
/// use num_traits::FromBytes;
150135
///
151-
/// # #[cfg(has_int_to_from_bytes)]
152-
/// # fn main() {
153136
/// #[cfg(target_endian = "big")]
154137
/// let bytes = [0x12, 0x34, 0x56, 0x78];
155138
///
156-
/// #[cfg(not(target_endian = "big"))]
139+
/// #[cfg(target_endian = "little")]
157140
/// let bytes = [0x78, 0x56, 0x34, 0x12];
158141
///
159-
/// let value = <u32 as FromBytes>::from_ne_bytes(&bytes);
142+
/// let value: u32 = FromBytes::from_ne_bytes(&bytes);
160143
/// assert_eq!(value, 0x12345678)
161-
/// # }
162-
/// # #[cfg(not(has_int_to_from_bytes))]
163-
/// # fn main() {}
164144
/// ```
165145
fn from_ne_bytes(bytes: &Self::Bytes) -> Self {
166146
#[cfg(target_endian = "big")]

0 commit comments

Comments
 (0)