I need help to call a function has two parametres (usize, bool) and use it as u64

Hello everyone, I have this function get_good_index and i want to use it here and replace this &(mint_number + 1).to_string() with it to get random index.

pub fn get_good_index(
    arr: &mut RefMut<&mut [u8]>,
    items_available: usize,
    index: usize,
    pos: bool,
) -> Result<(usize, bool)> {
    let mut index_to_use = index;
    let mut taken = 1;
    let mut found = false;
    let bit_mask_vec_start = CONFIG_ARRAY_START
        + 4
        + (items_available) * CONFIG_LINE_SIZE
        + 4
        + items_available
            .checked_div(8)
            .ok_or(CandyError::NumericalOverflowError)?
        + 4;

    while taken > 0 && index_to_use < items_available {
        let my_position_in_vec = bit_mask_vec_start
            + index_to_use
                .checked_div(8)
                .ok_or(CandyError::NumericalOverflowError)?;
        if arr[my_position_in_vec] == 255 {
            let eight_remainder = 8 - index_to_use
                .checked_rem(8)
                .ok_or(CandyError::NumericalOverflowError)?;
            let reversed = 8 - eight_remainder + 1;
            if (eight_remainder != 0 && pos) || (reversed != 0 && !pos) {
                if pos {
                    index_to_use += eight_remainder;
                } else {
                    if index_to_use < 8 {
                        break;
                    }
                    index_to_use -= reversed;
                }
            } else if pos {
                index_to_use += 8;
            } else {
                index_to_use -= 8;
            }
        } else {
            let position_from_right = 7 - index_to_use
                .checked_rem(8)
                .ok_or(CandyError::NumericalOverflowError)?;
            let mask = u8::pow(2, position_from_right as u32);

            taken = mask & arr[my_position_in_vec];

            match taken {
                x if x > 0 => {
                    if pos {
                        index_to_use += 1;
                    } else {
                        if index_to_use == 0 {
                            break;
                        }
                        index_to_use -= 1;
                    }
                }
                0 => {
                    found = true;
                    arr[my_position_in_vec] |= mask;
                }
                _ => (),
            }
        }
    }
    Ok((index_to_use, found))
}

I need to use get_good_index on place of the mint_number

pub fn get_config_line(
    a: &Account<'_, CandyMachine>,
    index: usize,
    mint_number: u64,
) -> Result<ConfigLine> {
    if let Some(hs) = &a.data.hidden_settings {
        return Ok(ConfigLine {
            name: hs.name.clone() + "#" + &(mint_number + 1).to_string(),
            uri: hs.uri.clone(),
        });
    }

What is your question?

I want to replace this &(mint_number + 1).to_string() with function get_good_index because i think is corespondent to provide a randomly index.

You can pass the function get_good_index as a parameter to get_config_line and call it from there.

2 Likes

I don't know how to do that, Please if you can help me.

Nvm., I just looked into the links that you shared in your first post, and the functions come from a library. You can't modify code from an imported library.

What you want to do cannot be done.

Oh i got it, And if i want make this mint_number:u64 provide randomly numbers on place to every time add one mint_number + 1

get_good_index returns a tuple of (usize, bool). You can convert the usize to u64 and provide it to get_config_line as the mint_number.

Yes this is what i'm looking for but i'm new on rust so i can't handle this by myself please if you can help to that. Thanks in advance.

I don't know all the details of your code, but what I said before would be done like this:

  let number_from_good_index = get_good_index(...).unwrap().0; // <- Here we are executing get_good_index and taking the first element (index 0) from the returned tuple. Note that this will panic if the Result is an Error because of the call to unwrap.

  let config_line = get_config_line(..., number_from_good_index as u64).unwrap(); // <- Here we pass number_from_good_index and explicitly cast it as u64.
1 Like

So i can use it like this ?

pub fn get_config_line(
    a: &Account<'_, CandyMachine>,
    index: usize,
    mint_number: u64,
) -> Result<ConfigLine> {
    if let Some(hs) = &a.data.hidden_settings {
    ```
    let number_from_good_index = get_good_index(...).unwrap().0;
    ```
        return Ok(ConfigLine {
            name: hs.name.clone() + "#" + &(number_from_good_index as u64).unwrap(),
            uri: hs.uri.clone(),
        });
    }

No. That's what I meant when I said a couple of posts above that what you wanted to do cannot be done.

You need to replace it at the call site.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.