pub fn pack(max_dim: u32, rects: &[(u32, u32)]) -> Option<Pack> {
        assert!(max_dim <= MAX_MAX_DIM && is_pow_2(max_dim));
        assert!(rects.iter().all(|r| 0 < r.0 && 0 < r.1));
        if rects.iter().any(|r| r.0 > max_dim || r.1 > max_dim) {
            return None;
        }

        let mut order_and_rects: Vec<_> = rects.iter().cloned().enumerate().collect();
        order_and_rects.sort_by_key(|&(_, (h, w))| u32::MAX - h * w);
        let order: Vec<_> = order_and_rects.iter().map(|&(idx, _)| idx).collect();
        let rects: Vec<_> = order_and_rects.iter().map(|&(_, dims)| dims).collect();

        (0..(log_ceil_pow_2(max_dim) + 1))
            .map(|lw| 1 << lw)
            .filter_map(|w| try_pack_rects(max_dim, w, &rects))
            .min_by_key(|pack| pack.area())
            .map(|mut pack| {
                permute(&mut pack.rects, &order);
                pack
            })
    }
// max_dim: maximum allowed height or width, must be power of 2
// rects: rectangle dimensions (height, width) to pack
maximum allowed height or width, must be power of 2
rects: rectangle dimensions (height, width) to pack
maximum allowed height or width must be power of 2 rects : rectangle dimensions ( height width ) to pack
pub fn pack(max_dim: u32, rects: &[(u32, u32)]) -> Option<Pack> {
        assert!(max_dim <= MAX_MAX_DIM && is_pow_2(max_dim));
        assert!(rects.iter().all(|r| 0 < r.0 && 0 < r.1));
        if rects.iter().any(|r| r.0 > max_dim || r.1 > max_dim) {
            return None;
        }
        let mut order_and_rects: Vec<_> = rects.iter().cloned().enumerate().collect();
        order_and_rects.sort_by_key(|&(_, (h, w))| u32::MAX - h * w);
        let order: Vec<_> = order_and_rects.iter().map(|&(idx, _)| idx).collect();
        let rects: Vec<_> = order_and_rects.iter().map(|&(_, dims)| dims).collect();
        (0..(log_ceil_pow_2(max_dim) + 1))
            .map(|lw| 1 << lw)
            .filter_map(|w| try_pack_rects(max_dim, w, &rects))
            .min_by_key(|pack| pack.area())
            .map(|mut pack| {
                permute(&mut pack.rects, &order);
                pack
            })
    }
pub fn pack ( max_dim : u32 , rects : & [ ( u32 , u32 ) ] ) -> Option < Pack > { assert ! ( max_dim MAX_MAX_DIM is_pow_2 ( max_dim ) ) ; assert ! ( rects iter ( ) all ( r 0 r 0 0 r 1 ) ) ; if rects . iter ( ) . any ( | r | r . 0 > max_dim || r . 1 > max_dim ) { return None ; } let mut order_and_rects : Vec < _ > = rects . iter ( ) . cloned ( ) . enumerate ( ) . collect ( ) ; order_and_rects . sort_by_key ( | & ( _ , ( h , w ) ) | u32 :: MAX - h * w ) ; let order : Vec < _ > = order_and_rects . iter ( ) . map ( | & ( idx , _ ) | idx ) . collect ( ) ; let rects : Vec < _ > = order_and_rects . iter ( ) . map ( | & ( _ , dims ) | dims ) . collect ( ) ; ( 0 .. ( log_ceil_pow_2 ( max_dim ) + 1 ) ) . map ( | lw | 1 << lw ) . filter_map ( | w | try_pack_rects ( max_dim , w , & rects ) ) . min_by_key ( | pack | pack . area ( ) ) . map ( | mut pack | { permute ( & mut pack . rects , & order ) ; pack } ) }
max_dim: maximum allowed height or width, must be power of 2
rects: rectangle dimensions (height, width) to pack
max_dim : maximum allowed height or width must be power of 2 rects : rectangle dimensions ( height width ) to pack
max_dim
u32
rects
&[(u32, u32)]
max_dim
u32
rects
&[(u32, u32)]