Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Deref trait for BigInt #770

Open
tcoratger opened this issue Feb 1, 2024 · 3 comments
Open

Implement Deref trait for BigInt #770

tcoratger opened this issue Feb 1, 2024 · 3 comments

Comments

@tcoratger
Copy link
Contributor

tcoratger commented Feb 1, 2024

Feature Request

Implement the Deref trait for the BigInt structure to allow users to conveniently access the underlying array.

Context

The Deref trait allows types to be treated like references. By implementing it for BigInt, users can easily access the underlying array when needed. This will simplifies part of the implementations where self.0 is used and can be replaced by self after the implementation.

Proposal

Add a Deref implementation for BigInt with the associated type Target set to [u64; N].

Example

use std::ops::Deref;

#[derive(Copy, Clone, PartialEq, Eq, Hash, Zeroize)]
pub struct BigInt<const N: usize>(pub [u64; N]);

impl<const N: usize> Deref for BigInt<N> {
    type Target = [u64; N];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

fn main() {
    let big_int = BigInt::<3>([1, 2, 3]);
    
    // Using Deref to access the underlying array
    let array_ref: &[u64; 3] = &*big_int;

    println!("{:?}", array_ref); // Output: [1, 2, 3]
}
@Pratyush
Copy link
Member

Pratyush commented Feb 1, 2024

This is behaviour that we want to hide. See, eg, #769

@tcoratger
Copy link
Contributor Author

This is behaviour that we want to hide. See, eg, #769

@Pratyush Great then if I understand well the proposed solution I imagine it will make sense to implement Deref for each individual Bs in order to manipulate them then more efficiently what do you think?

@burdges
Copy link
Contributor

burdges commented Feb 3, 2024

Deref seems poinless here since BigInt has a public field. In fact .0 winds up better since ownership can change.
https://github.com/arkworks-rs/algebra/blob/master/ff/src/biginteger/mod.rs#L33

We've all abused Deref of course, but ideally Deref should be reserved for smart pointers, and avoided for other types.
https://doc.rust-lang.org/stable/std/ops/trait.Deref.html
https://rust-unofficial.github.io/patterns/anti_patterns/deref.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants