Is there a way to build this struct on the heap or any alternative to this `large_stack_frames` issue?

⚓ rust    📅 2025-07-04    👤 surdeus    👁️ 3      

surdeus

I have this rust code:

#[derive(Debug, Default, Clone)]
pub struct PlayerFilters  {
    pub id: Option<IdFilter>,
    pub created_at: Option<DateTimeFilter>,
    pub created_by: Option<IdFilter>,
    pub position: Option<NumberFilter<i16>>,
    pub score: Option<NumberFilter<i16>>,
    pub and: Vec<Self>,
    pub or: Vec<Self>,
    pub not: Option<Box<Self>>,
    pub with_team: Option<TeamCondition>,
    // and a lot more fields here!
}

impl TryFrom<PlayerFilters> for Box<PlayerFilters> {
    type Error = CustomError;

    fn try_from(filters: PlayerFilters) -> Result<Self, Self::Error> {
        Ok(Self::new(PlayerFilters {
            id: filters.id.map(TryInto::try_into).transpose()?,
            created_at: filters.created_at,
            created_by: filters.created_by.map(TryInto::try_into).transpose()?,
            position: filters.position.map(TryInto::try_into).transpose()?,
            score: filters.score.map(TryInto::try_into).transpose()?,
            and: filters.and.map_or_else(
                || Ok(Vec::new()),
                |o| filters.into_iter().map(|item| item.try_into().map(|boxed: Self| *boxed)).collect::<Result<Vec<_>, _>>(),
            )?,
            or: filters.or.map_or_else(
                || Ok(Vec::new()),
                |o| filters.into_iter().map(|item| item.try_into().map(|boxed: Self| *boxed)).collect::<Result<Vec<_>, _>>(),
            )?,
            not: filters.not.map(|o| (*o).try_into()).transpose()?,
            with_team: filters.with_team.map(|o| (*o).try_into()).transpose()?,
            // and a lot more fields here!
        }))
    }
}

and I get this warning:

warning: this function may allocate 564738 bytes on the stack
391 |       fn try_from(o: PlayerFilters) -> Result<Self, Self::Error> {
    |          ^^^^^^^^
392 |           Ok(Self::new(PlayerFilters {
    |  ______________________-
393 | |             id: filters.id.map(TryInto::try_into).transpose()?,
394 | |             created_at: filters.created_at,
395 | |             created_by: filters.created_by.map(TryInto::try_into).transpose()?,
...   |
484 | |                 .transpose()?,
485 | |         }))
    | |_________- this is the largest part, at 92944 bytes for type `PlayerFilters`
    |
    = note: 564738 bytes is larger than Clippy's configured `stack-size-threshold` of 512000
    = note: allocating large amounts of stack space can overflow the stack and cause the program to abort
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#large_stack_frames
    = note: `-W clippy::large-stack-frames` implied by `-W clippy::nursery`
    = help: to override `-W clippy::nursery` add `#[allow(clippy::large_stack_frames)]`

Is there a way to avoid builind this huge struct on the stack building it directly on the heap?

Is there any alternative?

6 posts - 5 participants

Read full topic

🏷️ rust_feed