Git Product home page Git Product logo

Comments (1)

Esper89 avatar Esper89 commented on August 19, 2024 1

This would be very useful for const fns that sometimes need to handle runtime data too, such as a const fn method that needs to use a Cow<'static, [T]> field.

For example:

struct Foo(Cow<'static, [u32]>);

impl Foo {
    // Initialize with heap-allocated data at runtime.
    fn heap(data: Vec<u32>) -> Self {
        Foo(Cow::Owned(data))
    }

    // Initialize with static data at compile-time.
    const fn static(data: &'static [u32]) -> Self {
        Foo(Cow::Borrowed(data))
    }

    // Access the data, possibly at compile-time, possibly at runtime.
    const fn data(&self) -> &[u32] {
        match self.0 {
            Cow::Borrowed(slice) => slice,
            Cow::Owned(vec) => vec.as_slice(),
        }
    }
}

from rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.