@@ -839,3 +839,104 @@ mod prim_usize { }
839839/// locally known.
840840#[ stable( feature = "rust1" , since = "1.0.0" ) ]
841841mod prim_ref { }
842+
843+ #[ doc( primitive = "fn" ) ]
844+ //
845+ /// Function pointers, like `fn(usize) -> bool`.
846+ ///
847+ /// *See also the traits [`Fn`], [`FnMut`], and [`FnOnce`].*
848+ ///
849+ /// [`Fn`]: ops/trait.Fn.html
850+ /// [`FnMut`]: ops/trait.FnMut.html
851+ /// [`FnOnce`]: ops/trait.FnOnce.html
852+ ///
853+ /// Plain function pointers are obtained by casting either plain functions, or closures that don't
854+ /// capture an environment:
855+ ///
856+ /// ```
857+ /// fn add_one(x: usize) -> usize {
858+ /// x + 1
859+ /// }
860+ ///
861+ /// let ptr: fn(usize) -> usize = add_one;
862+ /// assert_eq!(ptr(5), 6);
863+ ///
864+ /// let clos: fn(usize) -> usize = |x| x + 5;
865+ /// assert_eq!(clos(5), 10);
866+ /// ```
867+ ///
868+ /// In addition to varying based on their signature, function pointers come in two flavors: safe
869+ /// and unsafe. Plain `fn()` function pointers can only point to safe functions,
870+ /// while `unsafe fn()` function pointers can point to safe or unsafe functions.
871+ ///
872+ /// ```
873+ /// fn add_one(x: usize) -> usize {
874+ /// x + 1
875+ /// }
876+ ///
877+ /// unsafe fn add_one_unsafely(x: usize) -> usize {
878+ /// x + 1
879+ /// }
880+ ///
881+ /// let safe_ptr: fn(usize) -> usize = add_one;
882+ ///
883+ /// //ERROR: mismatched types: expected normal fn, found unsafe fn
884+ /// //let bad_ptr: fn(usize) -> usize = add_one_unsafely;
885+ ///
886+ /// let unsafe_ptr: unsafe fn(usize) -> usize = add_one_unsafely;
887+ /// let really_safe_ptr: unsafe fn(usize) -> usize = add_one;
888+ /// ```
889+ ///
890+ /// On top of that, function pointers can vary based on what ABI they use. This is achieved by
891+ /// adding the `extern` keyword to the type name, followed by the ABI in question. For example,
892+ /// `fn()` is different from `extern "C" fn()`, which itself is different from `extern "stdcall"
893+ /// fn()`, and so on for the various ABIs that Rust supports. Non-`extern` functions have an ABI
894+ /// of `"Rust"`, and `extern` functions without an explicit ABI have an ABI of `"C"`. For more
895+ /// information, see [the nomicon's section on foreign calling conventions][nomicon-abi].
896+ ///
897+ /// [nomicon-abi]: ../nomicon/ffi.html#foreign-calling-conventions
898+ ///
899+ /// Extern function declarations with the "C" or "cdecl" ABIs can also be *variadic*, allowing them
900+ /// to be called with a variable number of arguments. Normal rust functions, even those with an
901+ /// `extern "ABI"`, cannot be variadic. For more information, see [the nomicon's section on
902+ /// variadic functions][nomicon-variadic].
903+ ///
904+ /// [nomicon-variadic]: ../nomicon/ffi.html#variadic-functions
905+ ///
906+ /// These markers can be combined, so `unsafe extern "stdcall" fn()` is a valid type.
907+ ///
908+ /// Like references in rust, function pointers are assumed to not be null, so if you want to pass a
909+ /// function pointer over FFI and be able to accomodate null pointers, make your type
910+ /// `Option<fn()>` with your required signature.
911+ ///
912+ /// Function pointers implement the following traits:
913+ ///
914+ /// * [`Clone`]
915+ /// * [`PartialEq`]
916+ /// * [`Eq`]
917+ /// * [`PartialOrd`]
918+ /// * [`Ord`]
919+ /// * [`Hash`]
920+ /// * [`Pointer`]
921+ /// * [`Debug`]
922+ ///
923+ /// [`Clone`]: clone/trait.Clone.html
924+ /// [`PartialEq`]: cmp/trait.PartialEq.html
925+ /// [`Eq`]: cmp/trait.Eq.html
926+ /// [`PartialOrd`]: cmp/trait.PartialOrd.html
927+ /// [`Ord`]: cmp/trait.Ord.html
928+ /// [`Hash`]: hash/trait.Hash.html
929+ /// [`Pointer`]: fmt/trait.Pointer.html
930+ /// [`Debug`]: fmt/trait.Debug.html
931+ ///
932+ /// Due to a temporary restriction in Rust's type system, these traits are only implemented on
933+ /// functions that take 12 arguments or less, with the `"Rust"` and `"C"` ABIs. In the future, this
934+ /// may change.
935+ ///
936+ /// In addition, function pointers of *any* signature, ABI, or safety are [`Copy`], and all *safe*
937+ /// function pointers implement [`Fn`], [`FnMut`], and [`FnOnce`]. This works because these traits
938+ /// are specially known to the compiler.
939+ ///
940+ /// [`Copy`]: marker/trait.Copy.html
941+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
942+ mod prim_fn { }
0 commit comments