Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions derive/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::str::from_utf8;

use proc_macro2::{Ident, Span, TokenStream};
use syn::{punctuated::Punctuated, spanned::Spanned, token::Comma, Data, Error, Field, Fields};

Expand Down Expand Up @@ -344,9 +342,8 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_path: &syn::Path) -> TokenS
Fields::Unnamed(ref fields) => {
let fields = &fields.unnamed;
let field_name = |i, _: &Option<Ident>| {
let data = stringify(i as u8);
let ident = from_utf8(&data).expect("We never go beyond ASCII");
let ident = Ident::new(ident, Span::call_site());
let ident =
Ident::new(&format!("__unnamed_field_{i}__"), Span::call_site());
quote!(#ident)
};

Expand Down Expand Up @@ -445,13 +442,3 @@ pub fn quote(data: &Data, type_name: &Ident, crate_path: &syn::Path) -> TokenStr
impl_encode(data, type_name, crate_path)
}
}

pub fn stringify(id: u8) -> [u8; 2] {
const CHARS: &[u8] = b"abcdefghijklmnopqrstuvwxyz";
let len = CHARS.len() as u8;
let symbol = |id: u8| CHARS[(id % len) as usize];
let a = symbol(id);
let b = symbol(id / len);

[a, b]
}
19 changes: 19 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,3 +978,22 @@ fn cow_str_decode_with_mem_tracking() {
let decoded = Cow::<'static, str>::decode_with_mem_limit(&mut &encoded[..], 6).unwrap();
assert_eq!(data, decoded);
}

#[test]
fn issue_747_enum_with_group_id_compiles() {
// Test case for issue #747: Clippy errors with 'casting usize to u8 may truncate the value'
pub type GroupId = u32;

#[derive(Debug, Clone, PartialEq, DeriveEncode, DeriveDecode, Default)]
pub enum PostVisibility {
#[default]
Public,
Supporter,
Group(GroupId),
}

let visibility = PostVisibility::Group(123);
let encoded = visibility.encode();
let decoded = PostVisibility::decode(&mut &encoded[..]).unwrap();
assert_eq!(visibility, decoded);
}
Loading