Sha256: 534e26201bd06cf5d824b2e2938109662fbffaea6af741fb06bf22fc77f5009f

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::{ule::VarULE, VarZeroSlice, VarZeroVec};
use databake::*;

impl<T: VarULE + ?Sized> Bake for VarZeroVec<'_, T> {
    fn bake(&self, env: &CrateEnv) -> TokenStream {
        env.insert("zerovec");
        if self.is_empty() {
            quote! { zerovec::VarZeroVec::new() }
        } else {
            let bytes = databake::Bake::bake(&self.as_bytes(), env);
            // Safe because self.as_bytes is a safe input
            quote! { unsafe { zerovec::VarZeroVec::from_bytes_unchecked(#bytes) } }
        }
    }
}

impl<T: VarULE + ?Sized> Bake for &VarZeroSlice<T> {
    fn bake(&self, env: &CrateEnv) -> TokenStream {
        env.insert("zerovec");
        if self.is_empty() {
            quote! { zerovec::VarZeroSlice::new_empty() }
        } else {
            let bytes = databake::Bake::bake(&self.as_bytes(), env);
            // Safe because self.as_bytes is a safe input
            quote! { unsafe { zerovec::VarZeroSlice::from_bytes_unchecked(#bytes) } }
        }
    }
}

#[test]
fn test_baked_vec() {
    test_bake!(
        VarZeroVec<str>,
        const: crate::VarZeroVec::new(),
        zerovec
    );

    test_bake!(
        VarZeroVec<str>,
        const: unsafe {
            crate::VarZeroVec::from_bytes_unchecked(
                b"\x02\0\0\0\0\0\x05\0helloworld"
            )
        },
        zerovec
    );
}

#[test]
fn test_baked_slice() {
    test_bake!(
        &VarZeroSlice<str>,
        const: crate::VarZeroSlice::new_empty(),
        zerovec
    );
    test_bake!(
        &VarZeroSlice<str>,
        const: unsafe {
            crate::VarZeroSlice::from_bytes_unchecked(
                b"\x02\0\0\0\0\0\x05\0helloworld"
            )
        },
        zerovec
    );
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/zerovec-0.10.4/src/varzerovec/databake.rs
wasmtime-28.0.0 ./ext/cargo-vendor/zerovec-0.10.4/src/varzerovec/databake.rs
wasmtime-27.0.0 ./ext/cargo-vendor/zerovec-0.10.4/src/varzerovec/databake.rs