Skip to content

Feature request: private_new that generates a private new function #211

@futile

Description

@futile

I would like an attribute, e.g., called private_new, that limits the visibility of the generated new() function.
I want to use this to limit where a newtype can be instantiated.

Example:

mod inner {
  pub struct Foobar {
    // some fields etc.
  }
  
  #[nutype(private_new)] // <- new attribute used here
  pub struct TaggedFoobar(Foobar);
  
  pub fn get_tagged_foobar() -> TaggedFoobar {
    let foobar = get_foobar_from_somewhere();
  
    // `new` should only be callable here, so that `TaggedFoobar` is guaranteed to originate from this fn!
    TaggedFoobar::new(foobar)
  }
}

use inner::*;

fn should_not_compile() -> TaggedFoobar {
  let foobar = get_foobar_from_somewhere();

  // !! This should not compile, because `new` is private/not visible here !!
  TaggedFoobar::new(foobar)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions