How do I use an enum that contains fields with bytemuck

This is my enum

use anchor_lang::solana_program::pubkey::Pubkey;
use bytemuck;

#[repr(C)]
#[derive(Clone, Copy, Debug, Contiguous)]
pub enum PaymentOption {
    Native,
    Token { mint: Pubkey },
}

I have tried using the Contiguous trait but it doesn’t support fielded enums.

My Cargo.toml deps

[dependencies]
anchor-lang = "0.29.0"
bytemuck = "1.18.0"

Pubkey size is 32 bytes.

1

You can not implement Contiguous for your type.

The reason for this not that it can’t be derived, nothing stops you from manually implementing it. Instead the fact that Contiguous is an unsafe trait, which means that the implementor must conform to rules documented by the library.

To soundly implement Contiguous, you need the following (from bytemuck‘s docs):

  1. The size of C and C::Int must be the same, and neither may be a ZST.
    (Note: alignment is explicitly allowed to differ)

  2. C::Int must be a primitive integer, and not a wrapper type. In the
    future, this may be lifted to include cases where the behavior is
    identical for a relevant set of traits (Ord, arithmetic, …).

  3. All C::Ints which are in the inclusive range between C::MIN_VALUE
    and C::MAX_VALUE are bitwise identical to unique valid instances of
    C.

  4. There exist no instances of C such that their bitpatterns, when
    interpreted as instances of C::Int, fall outside of the MAX_VALUE /
    MIN_VALUE range — It is legal for unsafe code to assume that if it
    gets a C that implements Contiguous, it is in the appropriate range.

  5. Finally, you promise not to provide overridden implementations of
    Contiguous::from_integer and Contiguous::into_integer.

First, the enum would need to be converted from #[repr(C)] to #[repr(Int)] (with Int = u8 for example).

Such enums are stored as a discriminant followed by a C-union of C-structs of variants.

In our case, this would mean that everything with discriminant 0 would allow arbitrary values in the C-union. This however contradicts our rule #3: while all bit patterns correspond to a valid value, they are not unique.

This could be solved by swapping the field order of the enum, and setting Contiguous::MAX_VALUE to one more than the maximal bit pattern of discriminant 0, and thus allowing only one bit pattern for discriminant 1.

What finally dooms us is rule #2:

The type Int must be a primitive integer.
PubKey is #[repr(transparent) and bytemuck::Pod over an [u8, 32], but there is no primitive integer type larger than [u8; 33] to contain it.

Any option to implement it manually? E.g. see the Playground

use bytemuck; // 1.16.3

#[derive(Clone, Copy, Debug)]
struct Pubkey {
    _key: i32
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub enum PaymentOption {
    Native,
    Token { mint: Pubkey },
}

unsafe impl bytemuck::Contiguous for PaymentOption {
  type Int = i32;
  const MIN_VALUE: i32 = i32::MIN;
  const MAX_VALUE: i32 = i32::MAX;
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật