text:
#ifndef LWPANEL_H
#define LWPANEL_H

#include <lwserver.h>
#include <lwxpanel.h>
#define PANEL_SERVICES_NAME     "LWPanelServices"
#define LWPANELFUNCS_GLOBAL     "LWPanelServices"
#define LWRASTERFUNCS_GLOBAL    "RasterServices"
#define LWCONTEXTMENU_GLOBAL    "ContextMenuServices"
#define LWPANELS_API_VERSION    19

typedef void *LWControlID;
typedef void *LWPanelID;
typedef void *LWContextMenuID;

typedef unsigned short   LWDualKey;

typedef struct st_display_Metrics {
        int              width, height;
        int              pixX, pixY;
        int              maxColors, depth;
        int              textHeight;
        int              textAscent;
} display_Metrics;

#define LWDisplayMetrics display_Metrics

/* Values are exchanged with controls by means of these wrappers  */
typedef int             LWType;

#define LWT_NULL         0
#define LWT_STRING       1
#define LWT_INTEGER      2
#define LWT_FLOAT        3
#define LWT_DISTANCE     4
#define LWT_VINT         5
#define LWT_VFLOAT       6
#define LWT_VDIST        7
#define LWT_BOOLEAN      8
#define LWT_CHOICE       9
#define LWT_SURFACE      10
#define LWT_FONT         11
#define LWT_TEXT         12
#define LWT_LAYERS       13
#define LWT_CUSTOM       14
#define LWT_RANGE        15
#define LWT_LWITEM       16
#define LWT_PERCENT      17
#define LWT_POPUP        18
#define LWT_AREA         19
#define LWT_XPANEL       20
#define LWT_TREE         21
#define LWT_MLIST        22
#define LWT__LAST        LWT_MLIST

typedef struct st_LWValString {
        LWType         type;
        char            *buf;
        int              bufLen;
} LWValString;

typedef struct st_LWValInt {
        LWType         type;
        int              value;
        int              defVal;
} LWValInt;

typedef struct st_LWValFloat {
        LWType         type;
        double           value;
        double           defVal;
} LWValFloat;

typedef struct st_LWValIVector {
        LWType         type;
        int              val[3];
        int              defVal;
} LWValIVector;

typedef struct st_LWValFVector {
        LWType         type;
        double           val[3];
        double           defVal;
} LWValFVector;

typedef struct st_LWValCustom {
        LWType         type;
        int              val[4];
} LWValCustom;

typedef struct st_LWValPointer {
        LWType         type;
        void           *ptr;
} LWValPointer;

typedef struct st_LWPanStringDesc {
        LWType       type;
        int          width;
} LWPanStringDesc;

typedef struct st_LWPanChoiceDesc {
        LWType       type;
        const char   **items;
        int          vertical;
} LWPanChoiceDesc;

typedef struct st_LWPanTextDesc {
        LWType       type;
        const char   **text;
} LWPanTextDesc;

typedef struct st_LWPanRangeDesc {
        LWType          type;
        int                     width;
        int                     min;
        int                     max;
} LWPanRangeDesc;

typedef struct st_LWPanAreaDesc {
        LWType          type;
        int                     width;
        int                     height;
} LWPanAreaDesc;

typedef struct st_LWPanLWItemDesc {
        LWType           type;
        void            *global;
        int              itemType;
        char            **list;
        int              count;
        int              width;
} LWPanLWItemDesc;

typedef struct st_LWPanPopupDesc {
        LWType           type;
        int              width;
        int              (*countFn)(void *);
        char            *(*nameFn)(void *, int);
} LWPanPopupDesc;

typedef struct st_LWPanListBoxDesc {
        LWType                   type;
        int                      width,visItems, top;  // width in pixels, height in items, top visible item
        int                      (*countFn)(void *);
        char                    *(*nameFn)(void *, int);
} LWPanListBoxDesc;

typedef struct st_LWPanMultiListBoxDesc {
        LWType                   type;
        int                      width,visItems, top;  // width in pixels, height in items, top visible item
        int                      (*countFn)(void *);
        char                    *(*nameFn)(void *, int, int); // item, column
        int                                              (*colWidth)(void *, int i);  // pixel width of column i, up to 8 columns terminate with 0
} LWPanMultiListBoxDesc;

typedef struct st_LWPanXPanDesc {
        LWType                   type;
        int                                              width, height;
        void                    *xpan;
} LWPanXPanDesc;

typedef struct st_LWPanTreeDesc {
        LWType                   type;
        int                      width, height;
        int                      (*countFn)(void *data, void *node); /* return number of leafs under given node, NULL node is root */
        void                    *(*leafFn)(void *data, void *node, int i); /* return leaf i of node, NULL node is root */
        char                    *(*infoFn)(void *data, void *node, int *nodeFlags); /* return "name" for node, fill 0 flags, or save non-zero flags */
        void                     (*moveFn)(void *data, void *node, void *parentNode, int i); /* NULL prevents moves, or called when user moves node, with new parent, position */
} LWPanTreeDesc;
typedef enum {   /* Tags to get/set panel attributes */
        PAN_X, PAN_Y, PAN_W, PAN_H, PAN_MOUSEX, PAN_MOUSEY,
        PAN_FLAGS, PAN_USERDRAW, PAN_USERACTIVATE,
        PAN_USEROPEN, PAN_USERCLOSE, PAN_TITLE,PAN_USERDATA, PAN_USERKEYS,
        PAN_LWINSTANCE, PAN_MOUSEBUTTON, PAN_MOUSEMOVE  ,PAN_PANFUN, PAN_VERSION, PAN_RESULT,
        PAN_HOSTDISPLAY, PAN_USERKEYUPS, PAN_QUALIFIERS, PAN_TO_FRONT
} pTag;

typedef enum { DR_ERASE, DR_RENDER, DR_GHOST, DR_REFRESH } DrMode;

typedef enum {           /* Tags to get/set panel attributes */
        CTL_X, CTL_Y, CTL_W, CTL_H, CTL_VALUE,
        CTL_HOTX, CTL_HOTY, CTL_HOTW, CTL_HOTH, CTL_LABELWIDTH,
        CTL_MOUSEX, CTL_MOUSEY,
        CTL_FLAGS, CTL_USERDRAW, CTL_USEREVENT, CTL_LABEL, CTL_USERDATA,
        CTL_PANEL, CTL_PANFUN,  /* use these 2 to get your panelID and functions! */
        CTL_RANGEMIN, CTL_RANGEMAX,             /* get/set slider range params  API 10 and later */
        CTL_ACTIVATE   /*  set only, to actvate/enter string controls   API 10 and later */
} cTag;

typedef enum { MX_HCHOICE, MX_VCHOICE ,MX_POPUP } MxType;

typedef void (*LWPanHook)(LWPanelID, void *);
typedef void (*LWPanKeyHook)(LWPanelID, void *, LWDualKey);
typedef void (*LWPanMouseHook)(LWPanelID, void *, int, int, int);  /* see input qualifier bits     */
typedef void (*LWPanDrawHook)(LWPanelID, void *, DrMode);
typedef void (*LWCtlDrawHook)(LWControlID, void *,DrMode);
typedef void (*LWCtlEventHook)(LWControlID, void *);

typedef union un_LWValue {
        LWType         type;
        LWValString      str;
        LWValInt         intv;
        LWValFloat       flt;
        LWValIVector     ivec;
        LWValFVector     fvec;
        LWValPointer     ptr;
        LWValCustom      cust;
} LWValue;

typedef union un_LWPanControlDesc {
        LWType                  type;
        LWPanStringDesc         string;
        LWPanChoiceDesc         choice;
        LWPanTextDesc           text;
        LWPanRangeDesc          range;
        LWPanAreaDesc           area;
        LWPanLWItemDesc         lwitem;
        LWPanPopupDesc          popup;
        LWPanListBoxDesc        listbox;
        LWPanXPanDesc           xpanel;
        LWPanTreeDesc           tree;
        LWPanMultiListBoxDesc   multiList;
} LWPanControlDesc;

typedef LWPanControlDesc                ControlDesc;
typedef struct st_DrawFuncs {
        void    (*drawPixel)(LWPanelID,int,int,int);                    /* color,x,y    */
        void    (*drawRGBPixel)(LWPanelID,int,int,int,int,int);                 /* r,g,b,x,y    */
        void    (*drawLine)(LWPanelID,int,int,int,int,int);             /* color,x,y,x2,y2      */
        void    (*drawBox)(LWPanelID,int,int,int,int,int);              /* color,x,y,w,h        */
        void    (*drawRGBBox)(LWPanelID,int,int,int,int,int,int,int);   /* r,g,b,x,y,w,h        */
        void    (*drawBorder)(LWPanelID,int,int,int,int,int);                   /* indent,x,y,w,h       (h==0 -> divider)       */
        int     (*textWidth)(LWPanelID,char *);                                                 /* text */
        void    (*drawText)(LWPanelID,char *,int,int,int);              /* text,color,x,y */
        const LWDisplayMetrics   *(*dispMetrics)();
} DrawFuncs;

typedef struct st_LWControl {
        void            (*draw)(LWControlID, DrMode);
        void            (*get)(LWControlID, cTag, LWValue *);
        void            (*set)(LWControlID, cTag, LWValue *);
        void            *priv_data;
} LWControl;

typedef struct st_LWPanelFuncs {
        LWPanelID       (*create)(char *, void *);
        void                    (*destroy)(LWPanelID);
        int                     (*open)(LWPanelID,int);  /* flags int (see flag bits  PANF_ etc.) */
        int                     (*handle)(LWPanelID, int);       /*  process input manually after non-blocking open, use EVNT_ bits to process async. or synchronously */
        void                    (*draw)(LWPanelID, DrMode);
        void                    (*close)(LWPanelID);
        void                    (*get)(LWPanelID, pTag, void *);
        void                    (*set)(LWPanelID, pTag, void *);
        LWControl       *(*addControl)(LWPanelID pan, char *type, ControlDesc *data, char *label);
        LWControl       *(*nextControl)(LWPanelID pan, LWControlID ctl);
        DrawFuncs       *drawFuncs;
        void                    *user_data;      /* do whatcha like */
        GlobalFunc      *globalFun;      /* please set this to your GlobalFunction pointer.. at least for file req. */
} LWPanelFuncs;

typedef void    *LWRasterID;

typedef struct st_LWRasterFuncs {
        void            (*drawPixel)(LWRasterID,int,int,int);               /* color,x,y    */
        void            (*drawRGBPixel)(LWRasterID,int,int,int,int,int);    /* r,g,b,x,y    */
        void            (*drawLine)(LWRasterID,int,int,int,int,int);        /* color,x,y,x2,y2      */
        void            (*drawBox)(LWRasterID,int,int,int,int,int);         /* color,x,y,w,h        */
        void            (*drawRGBBox)(LWRasterID,int,int,int,int,int,int,int);      /* r,g,b,x,y,w,h        */
        void            (*eraseBox)(LWRasterID,int,int,int,int);            /* x,y,w,h        */
        void            (*drawBorder)(LWRasterID,int,int,int,int,int);      /* indent,x,y,w,h       (h==0 -> divider)       */
        void            (*drawText)(LWRasterID,char *,int,int,int);         /* text,color,x,y */

        LWRasterID      (*create)(int,int,int);                                                         /* w, h, flags */
        void            (*destroy)(LWRasterID);
        void            (*blitPanel)(LWRasterID, int, int, LWPanelID, int,int,int,int);  /* src, srcx, srcy, dest, destx, desty, w, h*/

} LWRasterFuncs;

typedef struct st_ContextMenuFuncs {
        LWContextMenuID (*cmenuCreate)(LWPanPopupDesc *desc, void *userdata);
        int             (*cmenuDeploy)(LWContextMenuID menu, LWPanelID pan, int item); // item==-1 for no choice initially and/or on return
        void            (*cmenuDestroy)(LWContextMenuID menu);
} ContextMenuFuncs;

#define RGB_(r,g,b)     ((long)(((r) << 16) | ((b) << 8) | (g) | 0x1000000))
#define PANF_BLOCKING           1  /* open waits for close, or returns immediately */
#define PANF_CANCEL             2  /* Continue or Continue/Cancel*/
#define PANF_FRAME              4  /* Windowing System Title bar */
#define PANF_MOUSETRAP          8  /* Please track mouse input and call mouse callbacks */
#define PANF_PASSALLKEYS        16 /* Pass Enter/Esc keys to panel, instead of closing with OK/Cancel */
#define PANF_ABORT              32 /* Abort instead of Continue, usw w/ PANF_CANEL!! */
#define PANF_NOBUTT             64  /* No Continue or Continue/Cancel*/
#define PANF_RESIZE             128 /* Allow resizing of window. Clients should reposition/recreate controls */

#define CTLF_DISABLE            0x100    // Read-Only ,i.e. check if button has been drawn w/DR_GHOST , or erase
#define CTLF_INVISIBLE          0x200 // Read-Only        ... erased
#define CTLF_GHOST              CTLF_DISABLE

#define LWI_IMAGE               LWI_BONE+1
#define LWI_ANY                 LWI_IMAGE+1
#define NODE_FLAG_EXPND         1       /* TreeControl item expanded state*/
#define NODE_FLAG_WRITE         1024    /* TreeControl flag write-mode, allows clearing of other bits*/

#define EVNT_BLOCKING           1 /* 1  << 0  i.e. bit flags  handle() method waits for input, or gives immediate return */
#define EVNT_ALL                2 /* 1 << 1  if set, handle() method will poll entire queue of events,  */
                                                  /* w/EVNT_BLOCKING handle() won't return 'til panel is closed , w/out EVNT_BLOCKING, EVNT_ALL is irrelevant */

typedef int              InputMode;

#define TRUECOLOR(dm)   (dm->depth ? 1:0)
#define PALETTESIZE(dm) (dm->depth ? 1<<24:1<<dm->depth)
#define PAN_CREATE(pfunc,tit) ((*pfunc->create)(tit,NULL))
#define PAN_POST(pfunc,pan) ((*pfunc->open)(pan,PANF_BLOCKING|PANF_CANCEL|PANF_FRAME))
#define PAN_KILL(pfunc,pan) ((*pfunc->destroy)(pan))

#define HCHOICE_CTL(pfunc,pan,tit,ls) (desc.type=LWT_CHOICE,desc.choice.vertical=MX_HCHOICE,\
                          desc.choice.items=ls,\
                          (*pfunc->addControl) (pan, "ChoiceControl", &desc, tit))
#define VCHOICE_CTL(pfunc,pan,tit,ls) (desc.type=LWT_CHOICE,desc.choice.vertical=MX_VCHOICE,\
                          desc.choice.items=ls,\
                          (*pfunc->addControl) (pan, "ChoiceControl", &desc, tit))
#define POPUP_CTL(pfunc,pan,tit,ls) (desc.type=LWT_CHOICE,desc.choice.items=ls,\
                          (*pfunc->addControl) (pan, "PopupControl", &desc, tit))
#define WPOPUP_CTL(pfunc,pan,tit,ls,w) (desc.type=LWT_CHOICE,desc.choice.vertical=w, desc.choice.items=ls,\
                          (*pfunc->addControl) (pan, "PopupControl", &desc, tit))
#define FLOAT_CTL(pfunc,pan,tit)  (desc.type=LWT_FLOAT,\
                                                                        (*pfunc->addControl) (pan, "FloatControl", &desc, tit))
/* RO means 'read-only', uneditable text gadget */
#define FLOATRO_CTL(pfunc,pan,tit)  (desc.type=LWT_FLOAT,\
                                                                        (*pfunc->addControl) (pan, "FloatInfoControl", &desc, tit))
#define FVEC_CTL(pfunc,pan,tit)         (desc.type=LWT_VFLOAT,\
                                                                        (*pfunc->addControl) (pan, "FVecControl", &desc, tit))
#define FVECRO_CTL(pfunc,pan,tit)       (desc.type=LWT_VFLOAT,\
                                                                        (*pfunc->addControl) (pan, "FVecInfoControl", &desc, tit))
#define DVEC_CTL(pfunc,pan,tit)         (desc.type=LWT_VDIST,\
                                                                        (*pfunc->addControl) (pan, "FVecControl", &desc, tit))
#define DIST_CTL(pfunc,pan,tit)   (desc.type=LWT_DISTANCE,\
                                                                        (*pfunc->addControl) (pan, "FloatControl", &desc, tit))

/* a popup list of LW items (i.e. lights, objects, bones, ..search 'LWI_' ) */
#define ITEM_CTL(pfunc,pan,tit,glb,typ)   (desc.type=LWT_LWITEM, desc.lwitem.global=glb, desc.lwitem.itemType=typ,\
                                                                        desc.lwitem.list=NULL, desc.lwitem.count=0,desc.lwitem.width=0, \
                                                                        (*pfunc->addControl) (pan, "LWListControl", &desc, tit) )
/* a popup list of LW items (i.e. lights, objects, bones, ..search 'LWI_' ) */
#define WITEM_CTL(pfunc,pan,tit,glb,typ,w)   (desc.type=LWT_LWITEM,desc.lwitem.global=glb, desc.lwitem.itemType=typ,\
                                                                        desc.lwitem.list=NULL,desc.lwitem.count=0,desc.lwitem.width=w,  \
                                                                        (*pfunc->addControl) (pan, "LWListControl", &desc, tit) )
#define PIKITEM_CTL(pfunc,pan,tit,glb,typ,w)   (desc.type=LWT_LWITEM,desc.lwitem.global=glb, desc.lwitem.itemType=typ,\
                                                                        desc.lwitem.list=NULL,desc.lwitem.count=0,desc.lwitem.width=w,  \
                                                                        (*pfunc->addControl) (pan, "LWPickListControl", &desc, tit) )
/* a 'do something now' button */
#define BUTTON_CTL(pfunc,pan,tit)   (desc.type=LWT_INTEGER,\
                                                                        (*pfunc->addControl) (pan, "ButtonControl", &desc, tit))
#define WBUTTON_CTL(pfunc,pan,tit,w)   (desc.type=LWT_AREA,desc.area.width=w,desc.area.height=0,\
                                                                        (*pfunc->addControl) (pan, "ButtonControl", &desc, tit))
/* a checkbox */
#define BOOL_CTL(pfunc,pan,tit)   (desc.type=LWT_BOOLEAN,\
                                                                        (*pfunc->addControl) (pan, "BoolControl", &desc, tit))
/* a selectable text button */
#define BOOLBUTTON_CTL(pfunc,pan,tit)   (desc.type=LWT_BOOLEAN,\
                                                                        (*pfunc->addControl) (pan, "BoolButtonControl", &desc, tit))
#define WBOOLBUTTON_CTL(pfunc,pan,tit,w)   (desc.type=LWT_AREA,desc.area.width=w,desc.area.height=0,\
                                                                        (*pfunc->addControl) (pan, "BoolButtonControl", &desc, tit))
#define INT_CTL(pfunc,pan,tit)    (desc.type=LWT_INTEGER,\
                                                                        (*pfunc->addControl) (pan, "NumControl", &desc, tit))
#define INTRO_CTL(pfunc,pan,tit)    (desc.type=LWT_INTEGER,\
                                                                        (*pfunc->addControl) (pan, "NumInfoControl", &desc, tit))

#define SLIDER_CTL(pfunc,pan,tit,w,mn,mx)    (desc.type=LWT_RANGE,desc.range.width=w,\
                                                                        desc.range.min=mn,desc.range.max=mx,\
                                                                        (*pfunc->addControl)(pan, "SliderControl", &desc, tit))

#define VSLIDER_CTL(pfunc,pan,tit,w,mn,mx)    (desc.type=LWT_RANGE,desc.range.width=w,\
                                                                        desc.range.min=mn,desc.range.max=mx,\
                                                                        (*pfunc->addControl)(pan, "VSlideControl", &desc, tit))

#define HSLIDER_CTL(pfunc,pan,tit,w,mn,mx)    (desc.type=LWT_RANGE,desc.range.width=w,\
                                                                        desc.range.min=mn,desc.range.max=mx,\
                                                                        (*pfunc->addControl)(pan, "HSlideControl", &desc, tit))

#define UNSLIDER_CTL(pfunc,pan,tit,w,mn,mx)    (desc.type=LWT_RANGE,desc.range.width=w,\
                                                                        desc.range.min=mn,desc.range.max=mx,\
                                                                        (*pfunc->addControl)(pan, "UnboundSlideControl", &desc, tit))

#define MINISLIDER_CTL(pfunc,pan,tit,w,mn,mx)    (desc.type=LWT_RANGE,desc.range.width=w,\
                                                                        desc.range.min=mn,desc.range.max=mx,\
                                                                        (*pfunc->addControl)(pan, "MiniSliderControl", &desc, tit))

#define IVEC_CTL(pfunc,pan,tit)    (desc.type=LWT_VINT,\
                                                                        (*pfunc->addControl) (pan, "VecControl", &desc, tit))
#define IVECRO_CTL(pfunc,pan,tit)    (desc.type=LWT_VINT,\
                                                                        (*pfunc->addControl) (pan, "VecInfoControl", &desc, tit))
#define RGB_CTL(pfunc,pan,tit)    (desc.type=LWT_VINT,\
                                                                        (*pfunc->addControl) (pan, "RGBControl", &desc, tit))
#define MINIRGB_CTL(pfunc,pan,tit)    (desc.type=LWT_VINT,\
                                                                        (*pfunc->addControl) (pan, "MiniRGBControl", &desc, tit))
#define MINIHSV_CTL(pfunc,pan,tit)    (desc.type=LWT_VINT,\
                                                                        (*pfunc->addControl) (pan, "MiniHSVControl", &desc, tit))
#define RGBVEC_CTL(pfunc,pan,tit)    (desc.type=LWT_VINT,\
                                                                        (*pfunc->addControl) (pan, "RGBVecControl", &desc, tit))
#define STR_CTL(pfunc,pan,tit,w)        (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "EditControl", &desc, tit))
#define STRRO_CTL(pfunc,pan,tit,w)      (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "InfoControl", &desc, tit))
/* file name string + file-requester ! */
#define FILE_CTL(pfunc,pan,tit,w)       (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "FileControl", &desc, tit))
/* width in pixels not char.s here */
#define FILEBUTTON_CTL(pfunc,pan,tit,w)         (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "FileButtonControl", &desc, tit))
#define LOAD_CTL(pfunc,pan,tit,w)       (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "LoadControl", &desc, tit))
#define LOADBUTTON_CTL(pfunc,pan,tit,w)         (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "LoadButtonControl", &desc, tit))
#define SAVE_CTL(pfunc,pan,tit,w)       (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "SaveControl", &desc, tit))
#define SAVEBUTTON_CTL(pfunc,pan,tit,w)         (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "SaveButtonControl", &desc, tit))
#define DIR_CTL(pfunc,pan,tit,w)       (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "DirControl", &desc, tit))
#define DIRBUTTON_CTL(pfunc,pan,tit,w)         (desc.type=LWT_STRING,desc.string.width=w,\
                          (*pfunc->addControl) (pan, "DirButtonControl", &desc, tit))

#define TEXT_CTL(pfunc,pan,tit,sts)     (desc.type=LWT_TEXT,desc.text.text=sts,\
                                                                        (*pfunc->addControl) (pan, "TextControl", &desc, tit))
#define AREA_CTL(pfunc,pan,tit,w,h)    (desc.type=LWT_AREA,desc.area.width=w,desc.area.height=h,\
                                                                        (*pfunc->addControl)(pan, "AreaControl", &desc, tit))
#define PALETTE_CTL(pfunc,pan,tit,w,h)    (desc.type=LWT_RANGE,desc.area.width=w,desc.area.height=h,\
                                                                        (*pfunc->addControl)(pan, "PaletteControl", &desc, tit))
#define OPENGL_CTL(pfunc,pan,tit,w,h)    (desc.type=LWT_AREA,desc.area.width=w,desc.area.height=h,\
                                                                        (*pfunc->addControl)(pan, "OpenGLControl", &desc, tit))
#define CUSTPOPUP_CTL(pfunc,pan,tit,w,name,num)    (desc.type=LWT_POPUP,desc.popup.width=w,desc.popup.nameFn=name,\
                                                                        desc.popup.countFn=num,\
                                                                        (*pfunc->addControl)(pan, "CustomPopupControl", &desc, tit))
#define CANVAS_CTL(pfunc,pan,tit,w,h)    (desc.type=LWT_AREA,desc.area.width=w,desc.area.height=h,\
                                                                        (*pfunc->addControl)(pan, "CanvasControl", &desc, tit))

#define POPDOWN_CTL(pfunc,pan,tit,ls)     (desc.type=LWT_CHOICE,desc.choice.vertical=MX_POPUP,\
                          desc.choice.items=ls,\
                          (*pfunc->addControl) (pan, "PopDownControl", &desc, tit))
/* get intvector w/(dx,dy,count)  */
#define DRAGBUT_CTL(pfunc,pan,tit,w,h)    (desc.type=LWT_AREA,desc.area.width=w,desc.area.height=h,(*pfunc->addControl)(pan, "DragButtonControl", &desc, tit))
#define VDRAGBUT_CTL(pfunc,pan,tit)    (desc.type=LWT_AREA,desc.area.width=0,desc.area.height=-1,(*pfunc->addControl)(pan, "DragButtonControl", &desc, tit))
#define HDRAGBUT_CTL(pfunc,pan,tit)    (desc.type=LWT_AREA,desc.area.width=-1,desc.area.height=0,(*pfunc->addControl)(pan, "DragButtonControl", &desc, tit))
#define DRAGAREA_CTL(pfunc,pan,tit,w,h)    (desc.type=LWT_AREA,desc.area.width=w,desc.area.height=h,(*pfunc->addControl)(pan, "DragAreaControl", &desc, tit))
#define LISTBOX_CTL(pfunc,pan,tit,w,vis,name,num)    (desc.type=LWT_POPUP,desc.listbox.width=w,desc.listbox.nameFn=name,\
                                                                        desc.listbox.countFn=num, desc.listbox.visItems=vis,\
                                                                        (*pfunc->addControl)(pan, "ListBoxControl", &desc, tit))

#define MULTILIST_CTL(pfunc,pan,tit,w,vis,name,num,colw)    (desc.type=LWT_MLIST,desc.multiList.width=w,desc.multiList.nameFn=name,\
                                                                        desc.multiList.countFn=num,desc.multiList.visItems=vis,desc.multiList.colWidth=colw,\
                                                                        (*pfunc->addControl)(pan, "MultiListBoxControl", &desc, tit))

#define PERCENT_CTL(pfunc,pan,tit)    (desc.type=LWT_RANGE,\
                                                                        desc.range.min=0,desc.range.max=100,\
                                                                        (*pfunc->addControl)(pan, "PercentControl", &desc, tit))

#define ANGLE_CTL(pfunc,pan,tit)    (desc.type=LWT_RANGE,\
                                                                        desc.range.min=0,desc.range.max=360,\
                                                                        (*pfunc->addControl)(pan, "AngleControl", &desc, tit))

#define BORDER_CTL(pfunc,pan,tit,w,h)    (desc.type=LWT_AREA,desc.area.width=w,desc.area.height=h,\
                                                                        (*pfunc->addControl)(pan, "BorderControl", &desc, tit))

#define TABCHOICE_CTL(pfunc,pan,tit,ls) (desc.type=LWT_CHOICE,desc.choice.vertical=MX_HCHOICE,\
                          desc.choice.items=ls,\
                          (*pfunc->addControl) (pan, "TabChoiceControl", &desc, tit))

#define CHANNEL_CTL(pfunc,pan,tit,w,h) (desc.type=LWT_AREA,desc.area.width=w,desc.area.height=h,\
                          (*pfunc->addControl) (pan, "ChannelControl", &desc, tit))

#define XPANEL_CTL(pfunc,pan,tit,xp) (desc.type=LWT_XPANEL, desc.xpanel.xpan=xp,\
                          (*pfunc->addControl) (pan, "XPanelControl", &desc, tit))

#define TREE_CTL(pfunc,pan,tit,w,h,info,num,node)    (desc.type=LWT_TREE,desc.tree.width=w,desc.tree.height=h,desc.tree.infoFn=info,\
                                                                        desc.tree.countFn=num,desc.tree.leafFn=node,desc.tree.moveFn=NULL,\
                                                                        (*pfunc->addControl)(pan, "TreeControl", &desc, tit))

/*
  The folowing macros require a few global LWValue variables initialized as
  appropriate types.
- - - - - - CUT HERE - - - - - */

/* required by macros in lwpanel.h */
/*static LWValue ival={LWT_INTEGER},ivecval={LWT_VINT},
  fval={LWT_FLOAT},fvecval={LWT_VFLOAT},sval={LWT_STRING}; */

/* - - - - - - CUT HERE - - - - - */

#define SET_STR(ctl,s,l) (sval.str.buf=s,sval.str.bufLen=l,(*ctl->set)(ctl, CTL_VALUE, &sval))
#define SET_INT(ctl,n) (ival.intv.value=n,(*ctl->set)(ctl, CTL_VALUE, &ival))
#define SET_FLOAT(ctl,f) (fval.flt.value=f,(*ctl->set)(ctl, CTL_VALUE, &fval))
#define SET_FVEC(ctl,x,y,z) (fvecval.fvec.val[0]=x,fvecval.fvec.val[1]=y,fvecval.fvec.val[2]=z,\
    (*ctl->set)(ctl, CTL_VALUE, &fvecval))
#define SET_IVEC(ctl,x,y,z) (ivecval.ivec.val[0]=x,ivecval.ivec.val[1]=y,ivecval.ivec.val[2]=z,\
    (*ctl->set)(ctl, CTL_VALUE, &ivecval))
#define SETV_FVEC(ctl,v) (fvecval.fvec.val[0]=v[0],fvecval.fvec.val[1]=v[1],fvecval.fvec.val[2]=v[2],\
    (*ctl->set)(ctl, CTL_VALUE, &fvecval))
#define SETV_IVEC(ctl,v) (ivecval.ivec.val[0]=v[0],ivecval.ivec.val[1]=v[1],ivecval.ivec.val[2]=v[2],\
    (*ctl->set)(ctl, CTL_VALUE, &ivecval))

#define GET_STR(ctl,s,l) (sval.str.buf=s,sval.str.bufLen=l,(*ctl->get)(ctl, CTL_VALUE, &sval))
#define GET_INT(ctl,n) ((*ctl->get)(ctl, CTL_VALUE, &ival),n=ival.intv.value)
#define GET_FLOAT(ctl,f) ((*ctl->get)(ctl, CTL_VALUE, &fval),f=fval.flt.value)
#define GET_FVEC(ctl,x,y,z) ((*ctl->get)(ctl, CTL_VALUE, &fvecval),x=fvecval.fvec.val[0],y=fvecval.fvec.val[1],z=fvecval.fvec.val[2])
#define GETV_FVEC(ctl,v) ((*ctl->get)(ctl, CTL_VALUE, &fvecval),v[0]=fvecval.fvec.val[0],v[1]=fvecval.fvec.val[1],v[2]=fvecval.fvec.val[2])
#define GET_IVEC(ctl,x,y,z) ((*ctl->get)(ctl, CTL_VALUE, &ivecval),x=ivecval.ivec.val[0],y=ivecval.ivec.val[1],z=ivecval.ivec.val[2])
#define GETV_IVEC(ctl,v) ((*ctl->get)(ctl, CTL_VALUE, &ivecval),v[0]=ivecval.ivec.val[0],v[1]=ivecval.ivec.val[1],v[2]=ivecval.ivec.val[2])

#define CON_X(ctl)      ((*ctl->get)(ctl, CTL_X, &ival),ival.intv.value)
#define CON_Y(ctl)      ((*ctl->get)(ctl, CTL_Y, &ival),ival.intv.value)
#define CON_W(ctl)      ((*ctl->get)(ctl, CTL_W, &ival),ival.intv.value)
#define CON_H(ctl)      ((*ctl->get)(ctl, CTL_H, &ival),ival.intv.value)
#define CON_LW(ctl)     ((*ctl->get)(ctl, CTL_LABELWIDTH, &ival),ival.intv.value)
#define CON_PAN(ctl)    ((*ctl->get)(ctl, CTL_PANEL, &ival),ival.ptr.ptr)
#define CON_PANFUN(ctl) ((*ctl->get)(ctl, CTL_PANFUN, &ival),ival.ptr.ptr)
#define CON_HOTX(ctl)   ((*ctl->get)(ctl, CTL_HOTX, &ival),ival.intv.value)
#define CON_HOTY(ctl)   ((*ctl->get)(ctl, CTL_HOTY, &ival),ival.intv.value)
#define CON_HOTW(ctl)   ((*ctl->get)(ctl, CTL_HOTW, &ival),ival.intv.value)
#define CON_HOTH(ctl)   ((*ctl->get)(ctl, CTL_HOTH, &ival),ival.intv.value)
#define CON_MOUSEX(ctl) ((*ctl->get)(ctl, CTL_MOUSEX, &ival),ival.intv.value)
#define CON_MOUSEY(ctl) ((*ctl->get)(ctl, CTL_MOUSEY, &ival),ival.intv.value)
#define MOVE_CON(ctl,x,y)       ( (ival.intv.value=x,(*ctl->set)(ctl, CTL_X, &ival)),(ival.intv.value=y,(*ctl->set)(ctl, CTL_Y, &ival)) )
#define CON_SETEVENT(ctl,f,d) ( (ival.ptr.ptr=(void*)d,(*ctl->set)(ctl,CTL_USERDATA,&ival)), (ival.ptr.ptr=(void*)f,(*ctl->set)(ctl,CTL_USEREVENT,&ival)) )
#define ERASE_CON(ctl)  ((*ctl->draw)(ctl,DR_ERASE))
#define REDRAW_CON(ctl) ((*ctl->draw)(ctl,DR_REFRESH))
#define GHOST_CON(ctl)  ((*ctl->draw)(ctl,DR_GHOST))
#define RENDER_CON(ctl) ((*ctl->draw)(ctl,DR_RENDER))
#define UNGHOST_CON(ctl) ((*ctl->draw)(ctl,DR_RENDER))
#define ACTIVATE_CON(ctl)       (ival.intv.value=0, (*ctl->set)(ctl, CTL_ACTIVATE, &ival))

#define PAN_GETX(pfunc,pan) ((*pfunc->get)(pan,PAN_X,(void *)&(ival.intv.value)),ival.intv.value)
#define PAN_GETY(pfunc,pan) ((*pfunc->get)(pan,PAN_Y,(void *)&(ival.intv.value)),ival.intv.value)
#define PAN_GETW(pfunc,pan) ((*pfunc->get)(pan,PAN_W,(void *)&(ival.intv.value)),ival.intv.value)
#define PAN_GETH(pfunc,pan) ((*pfunc->get)(pan,PAN_H,(void *)&(ival.intv.value)),ival.intv.value)
#define PAN_SETH(pfunc,pan,h)   ( ival.intv.value=h, (*pfunc->set)(pan, PAN_H, (void *)&ival.intv.value) )
#define PAN_SETW(pfunc,pan,w)   ( ival.intv.value=w,(*pfunc->set)(pan, PAN_W, (void *)&ival.intv.value) )
#define MOVE_PAN(pfunc,pan,x,y) ( ival.intv.value=x,(*pfunc->set)(pan, PAN_X, (void *)&ival.intv.value),ival.intv.value=y,(*pfunc->set)(pan, PAN_Y, (void *)&ival.intv.value) )
#define PAN_SETDATA(pfunc,pan,d)  ( (*pfunc->set)(pan,PAN_USERDATA,(void *)d) )
#define PAN_SETDRAW(pfunc,pan,d)  ( (*pfunc->set)(pan,PAN_USERDRAW,(void *)d) )
#define PAN_SETKEYS(pfunc,pan,d)  ( (*pfunc->set)(pan,PAN_USERKEYS,(void *)d) )
#define PAN_GETVERSION(pfunc,pan) ((*pfunc->get)(pan,PAN_VERSION,(void *)&(ival.intv.value)),ival.intv.value)

                /*      input qualifiers */
#define IQ_CTRL         1
#define IQ_SHIFT        2
#define IQ_ALT          4
#define IQ_CONSTRAIN    8
#define IQ_ADJUST       16
#define MOUSE_LEFT      32
#define MOUSE_MID       64
#define MOUSE_RIGHT     96
#define MOUSE_DOWN              128
#define RCLICK(code)            ( (code&MOUSE_LEFT) && (code&MOUSE_MID) ? 1:0)
#define LCLICK(code)            ( (code&MOUSE_LEFT) && (!(code&MOUSE_MID)) ? 1:0)
#define MCLICK(code)            ( (!(code&MOUSE_LEFT)) && (code&MOUSE_MID) ? 1:0)

#define PALETTESIZE(dm) (dm->depth ? 1<<24:1<<dm->depth)

                /* Input key codes */

#define LWDK_CHAR(c)      ((LWDualKey) (c))
#define LWDK_F1           ((LWDualKey) 0x1001)
#define LWDK_F2           ((LWDualKey) 0x1002)
#define LWDK_F3           ((LWDualKey) 0x1003)
#define LWDK_F4           ((LWDualKey) 0x1004)
#define LWDK_F5           ((LWDualKey) 0x1005)
#define LWDK_F6           ((LWDualKey) 0x1006)
#define LWDK_F7           ((LWDualKey) 0x1007)
#define LWDK_F8           ((LWDualKey) 0x1008)
#define LWDK_F9           ((LWDualKey) 0x1009)
#define LWDK_F10          ((LWDualKey) 0x100A)
#define LWDK_F11          ((LWDualKey) 0x100B)
#define LWDK_F12          ((LWDualKey) 0x100C)

#define LWDK_SC_LEFT      ((LWDualKey) 0x1010)
#define LWDK_SC_RIGHT     ((LWDualKey) 0x1011)
#define LWDK_SC_UP        ((LWDualKey) 0x1012)
#define LWDK_SC_DOWN      ((LWDualKey) 0x1013)

#define LWDK_DELETE       ((LWDualKey) 0x1020)
#define LWDK_HELP         ((LWDualKey) 0x1021)
#define LWDK_RETURN       ((LWDualKey) 0x1022)

#define LWDK_HOME      ((LWDualKey) 0x108F)
#define LWDK_END       ((LWDualKey) 0x1090)
#define LWDK_PAGEUP    ((LWDualKey) 0x1091)
#define LWDK_PAGEDOWN  ((LWDualKey) 0x1092)

#define LWDK_TOP_0        ((LWDualKey) 0x1030)
#define LWDK_TOP_1        ((LWDualKey) 0x1031)
#define LWDK_TOP_2        ((LWDualKey) 0x1032)
#define LWDK_TOP_3        ((LWDualKey) 0x1033)
#define LWDK_TOP_4        ((LWDualKey) 0x1034)
#define LWDK_TOP_5        ((LWDualKey) 0x1035)
#define LWDK_TOP_6        ((LWDualKey) 0x1036)
#define LWDK_TOP_7        ((LWDualKey) 0x1037)
#define LWDK_TOP_8        ((LWDualKey) 0x1038)
#define LWDK_TOP_9        ((LWDualKey) 0x1039)

#define LWDK_PAD_0        ((LWDualKey) 0x1040)
#define LWDK_PAD_1        ((LWDualKey) 0x1041)
#define LWDK_PAD_2        ((LWDualKey) 0x1042)
#define LWDK_PAD_3        ((LWDualKey) 0x1043)
#define LWDK_PAD_4        ((LWDualKey) 0x1044)
#define LWDK_PAD_5        ((LWDualKey) 0x1045)
#define LWDK_PAD_6        ((LWDualKey) 0x1046)
#define LWDK_PAD_7        ((LWDualKey) 0x1047)
#define LWDK_PAD_8        ((LWDualKey) 0x1048)
#define LWDK_PAD_9        ((LWDualKey) 0x1049)

#define LWDK_PAD_OPAREN   ((LWDualKey) 0x1050)
#define LWDK_PAD_CPAREN   ((LWDualKey) 0x1051)
#define LWDK_PAD_SLASH    ((LWDualKey) 0x1052)
#define LWDK_PAD_STAR     ((LWDualKey) 0x1053)
#define LWDK_PAD_DASH     ((LWDualKey) 0x1054)
#define LWDK_PAD_PLUS     ((LWDualKey) 0x1055)
#define LWDK_PAD_DECIMAL  ((LWDualKey) 0x1056)
#define LWDK_PAD_ENTER    ((LWDualKey) 0x1057)

#define LWDK_ALT          ((LWDualKey) 0x1060)
#define LWDK_SHIFT        ((LWDualKey) 0x1061)
#define LWDK_CTRL         ((LWDualKey) 0x1062)

                /* Color Definitions */
#define SYSTEM_Ic(i)    (4136 + i)
#define COLOR_BG        LWP_BG
#define COLOR_DK_YELLOW LWP_EDIT_IMG
#define COLOR_DK_GREY   LWP_SHADOW
#define COLOR_LT_YELLOW LWP_EDIT_SEL
#define COLOR_MD_GREY   LWP_B_FACE
#define COLOR_LT_GREY   LWP_GLINT
#define COLOR_BLACK     LWP_NORMAL
#define COLOR_WHITE     LWP_HEADLINE

#define LWP_NONE         SYSTEM_Ic(0)
#define LWP_INVERT       SYSTEM_Ic(1)
#define LWP_BG           SYSTEM_Ic(2)
#define LWP_NORMAL       SYSTEM_Ic(3)
#define LWP_HEADLINE     SYSTEM_Ic(4)
#define LWP_INFO_BG      SYSTEM_Ic(5)
#define LWP_INFO_IMG     SYSTEM_Ic(6)
#define LWP_INFO_DIS     SYSTEM_Ic(7)
#define LWP_EDIT_BG      SYSTEM_Ic(8)
#define LWP_EDIT_IMG     SYSTEM_Ic(9)
#define LWP_EDIT_SEL     SYSTEM_Ic(10)
#define LWP_EDIT_DIS     SYSTEM_Ic(11)

#define LWP_BLACK       SYSTEM_Ic(12)
#define LWP_SHADOW      SYSTEM_Ic(13)
#define LWP_LOWERED     SYSTEM_Ic(14)
#define LWP_RAISED      SYSTEM_Ic(15)
#define LWP_HILIGHT     SYSTEM_Ic(16)

#define LWP_0_STAT      SYSTEM_Ic(17)
#define LWP_1_STAT      SYSTEM_Ic(18)
#define LWP_2_STAT      SYSTEM_Ic(19)
#define LWP_3_STAT      SYSTEM_Ic(20)
#define LWP_4_STAT      SYSTEM_Ic(21)
#define LWP_5_STAT      SYSTEM_Ic(22)
#define LWP_0_DLOG      SYSTEM_Ic(23)
#define LWP_1_DLOG      SYSTEM_Ic(24)
#define LWP_2_DLOG      SYSTEM_Ic(25)
#define LWP_3_DLOG      SYSTEM_Ic(26)
#define LWP_4_DLOG      SYSTEM_Ic(27)
#define LWP_5_DLOG      SYSTEM_Ic(28)
#define LWP_0_DOIT      SYSTEM_Ic(29)
#define LWP_1_DOIT      SYSTEM_Ic(30)
#define LWP_2_DOIT      SYSTEM_Ic(31)
#define LWP_3_DOIT      SYSTEM_Ic(32)
#define LWP_4_DOIT      SYSTEM_Ic(33)
#define LWP_5_DOIT      SYSTEM_Ic(34)
#define LWP_0_DRAG      SYSTEM_Ic(35)
#define LWP_1_DRAG      SYSTEM_Ic(36)
#define LWP_2_DRAG      SYSTEM_Ic(37)
#define LWP_3_DRAG      SYSTEM_Ic(38)
#define LWP_4_DRAG      SYSTEM_Ic(39)
#define LWP_5_DRAG      SYSTEM_Ic(40)
#define LWP_1_BG        SYSTEM_Ic(41)
#define LWP_2_BG        SYSTEM_Ic(42)
#define LWP_3_BG        SYSTEM_Ic(43)

/*  Same Palette different names (based on defaults)!*/
#define LWP_GRAY1      SYSTEM_Ic( 7)
#define LWP_GRAY2      SYSTEM_Ic(13)
#define LWP_GRAY3      SYSTEM_Ic( 1)
#define LWP_GRAY4      SYSTEM_Ic( 8)
#define LWP_GRAY5      SYSTEM_Ic( 5)
#define LWP_GRAY6      SYSTEM_Ic( 2)
#define LWP_GRAY7      SYSTEM_Ic(11)
#define LWP_GRAY8      SYSTEM_Ic( 9)
#define LWP_WHITE      SYSTEM_Ic( 4)
#define LWP_CANARY1    SYSTEM_Ic(17)
#define LWP_CANARY2    SYSTEM_Ic(18)
#define LWP_CANARY3    SYSTEM_Ic(19)
#define LWP_CANARY4    SYSTEM_Ic(20)
#define LWP_CANARY5    SYSTEM_Ic(21)
#define LWP_CANARY6    SYSTEM_Ic(22)
#define LWP_AQUA1      SYSTEM_Ic(23)
#define LWP_AQUA2      SYSTEM_Ic(24)
#define LWP_AQUA3      SYSTEM_Ic(25)
#define LWP_AQUA4      SYSTEM_Ic(26)
#define LWP_AQUA5      SYSTEM_Ic(27)
#define LWP_AQUA6      SYSTEM_Ic(28)
#define LWP_LAVENDER1  SYSTEM_Ic(29)
#define LWP_LAVENDER2  SYSTEM_Ic(30)
#define LWP_LAVENDER3  SYSTEM_Ic(31)
#define LWP_LAVENDER4  SYSTEM_Ic(32)
#define LWP_LAVENDER5  SYSTEM_Ic(33)
#define LWP_LAVENDER6  SYSTEM_Ic(34)
#define LWP_OLIVE1     SYSTEM_Ic(35)
#define LWP_OLIVE2     SYSTEM_Ic(36)
#define LWP_OLIVE3     SYSTEM_Ic(37)
#define LWP_OLIVE4     SYSTEM_Ic(38)
#define LWP_OLIVE5     SYSTEM_Ic(39)
#define LWP_OLIVE6     SYSTEM_Ic(40)

#endif


text:
use mio::{event::Source, Events, Interest, Poll, Registry, Token};
use std::{
    fs::File,
    io, mem,
    os::unix::io::{AsRawFd, RawFd},
    pin::Pin,
    ptr,
};

mod util;
use util::{expect_events, expect_no_events, init, temp_file, ExpectEvent};

const UDATA: Token = Token(0xdead_beef);

/// A highly feature-incomplete POSIX AIO event source, suitable for testing
/// mio's handling of kqueue's EVFILT_AIO.
struct Aiocb(Pin<Box<libc::aiocb>>);

impl Aiocb {
    /// Constructs a new `Aiocb` with no associated data.
    ///
    /// The resulting `Aiocb` structure is suitable for use with `aio_fsync`
    pub fn from_fd(fd: RawFd) -> Aiocb {
        // Use mem::zeroed instead of explicitly zeroing each field, because the
        // number and name of reserved fields is OS-dependent.  On some OSes,
        // some reserved fields are used the kernel for state, and must be
        // explicitly zeroed when allocated.
        let mut inner = unsafe { mem::zeroed::<libc::aiocb>() };
        inner.aio_fildes = fd;
        inner.aio_sigevent.sigev_notify = libc::SIGEV_NONE;
        Aiocb(Box::pin(inner))
    }

    /// Constructs a new `Aiocb` suitable for writing to offset 0 of a file.
    #[cfg(target_os = "freebsd")]
    pub fn from_slice(fd: RawFd, buf: &[u8]) -> Aiocb {
        let mut aiocb = Aiocb::from_fd(fd);
        aiocb.0.aio_nbytes = buf.len();
        aiocb.0.aio_buf = buf.as_ptr() as *mut libc::c_void;
        aiocb
    }

    pub fn fsync(&mut self) -> io::Result<()> {
        unsafe {
            // Safe because we don't move the libc::aiocb
            let selfp = self.0.as_mut().get_unchecked_mut();
            match libc::aio_fsync(libc::O_SYNC, selfp) {
                0 => Ok(()),
                _ => Err(io::Error::last_os_error()),
            }
        }
    }
}

impl Source for Aiocb {
    fn register(
        &mut self,
        registry: &Registry,
        token: Token,
        interests: Interest,
    ) -> io::Result<()> {
        assert!(interests.is_aio());
        let udata = usize::from(token);
        let kq = registry.as_raw_fd();
        self.0.aio_sigevent.sigev_notify = libc::SIGEV_KEVENT;
        self.0.aio_sigevent.sigev_signo = kq;
        self.0.aio_sigevent.sigev_value.sival_ptr = udata as *mut libc::c_void;
        self.0.aio_sigevent.sigev_notify_thread_id = 0;
        Ok(())
    }

    fn reregister(
        &mut self,
        registry: &Registry,
        token: Token,
        interests: Interest,
    ) -> io::Result<()> {
        self.register(registry, token, interests)
    }

    fn deregister(&mut self, _registry: &Registry) -> io::Result<()> {
        self.0.aio_sigevent.sigev_notify = libc::SIGEV_NONE;
        self.0.aio_sigevent.sigev_value.sival_ptr = ptr::null_mut();
        Ok(())
    }
}

#[cfg(target_os = "freebsd")]
struct Liocb {
    _aiocbs: Box<[Aiocb]>,
    /// The actual list passed to `libc::lio_listio`.
    ///
    /// It must live for as long as any of the operations are still being
    /// processesed, because the aio subsystem uses its address as a unique
    /// identifier.
    list: Box<[*mut libc::aiocb]>,
    sev: libc::sigevent,
}

#[cfg(target_os = "freebsd")]
impl Liocb {
    fn listio(&mut self) -> io::Result<()> {
        unsafe {
            let r = libc::lio_listio(
                libc::LIO_NOWAIT,
                self.list.as_ptr(),
                self.list.len() as i32,
                &mut self.sev as *mut libc::sigevent,
            );
            match r {
                0 => Ok(()),
                _ => Err(io::Error::last_os_error()),
            }
        }
    }

    fn new(inputs: Vec<Aiocb>) -> Liocb {
        let mut aiocbs = inputs.into_boxed_slice();
        for aiocb in aiocbs.iter_mut() {
            aiocb.0.aio_lio_opcode = libc::LIO_WRITE;
        }
        let list = aiocbs
            .iter_mut()
            .map(|aiocb| &mut *aiocb.0 as *mut libc::aiocb)
            .collect::<Vec<_>>()
            .into_boxed_slice();
        let sev = unsafe { mem::zeroed::<libc::sigevent>() };
        Liocb {
            _aiocbs: aiocbs,
            list,
            sev,
        }
    }
}

#[cfg(target_os = "freebsd")]
impl Source for Liocb {
    fn register(
        &mut self,
        registry: &Registry,
        token: Token,
        interests: Interest,
    ) -> io::Result<()> {
        assert!(interests.is_lio());
        let udata = usize::from(token);
        let kq = registry.as_raw_fd();
        self.sev.sigev_notify = libc::SIGEV_KEVENT;
        self.sev.sigev_signo = kq;
        self.sev.sigev_value.sival_ptr = udata as *mut libc::c_void;
        self.sev.sigev_notify_thread_id = 0;
        Ok(())
    }

    fn reregister(
        &mut self,
        registry: &Registry,
        token: Token,
        interests: Interest,
    ) -> io::Result<()> {
        self.register(registry, token, interests)
    }

    fn deregister(&mut self, _registry: &Registry) -> io::Result<()> {
        self.sev.sigev_notify = libc::SIGEV_NONE;
        self.sev.sigev_value.sival_ptr = ptr::null_mut();
        Ok(())
    }
}

mod aio {
    use super::*;

    #[test]
    fn smoke() {
        init();
        let mut poll = Poll::new().unwrap();
        let mut events = Events::with_capacity(8);

        let f = File::create(temp_file("aio::smoke")).unwrap();
        let mut aiocb = Aiocb::from_fd(f.as_raw_fd());
        poll.registry()
            .register(&mut aiocb, UDATA, Interest::AIO)
            .unwrap();

        expect_no_events(&mut poll, &mut events);
        aiocb.fsync().unwrap();
        expect_events(
            &mut poll,
            &mut events,
            vec![ExpectEvent::new(UDATA, Interest::AIO)],
        );
    }
}

#[cfg(target_os = "freebsd")]
mod lio {
    use super::*;

    #[test]
    fn smoke() {
        init();
        let data = b"hello, world!\n";
        let mut poll = Poll::new().unwrap();
        let mut events = Events::with_capacity(8);

        let f0 = File::create(temp_file("lio::smoke0")).unwrap();
        let f1 = File::create(temp_file("lio::smoke1")).unwrap();
        let aiocb0 = Aiocb::from_slice(f0.as_raw_fd(), data);
        let aiocb1 = Aiocb::from_slice(f1.as_raw_fd(), data);
        let mut liocb = Liocb::new(vec![aiocb0, aiocb1]);
        poll.registry()
            .register(&mut liocb, UDATA, Interest::LIO)
            .unwrap();

        expect_no_events(&mut poll, &mut events);
        liocb.listio().unwrap();
        expect_events(
            &mut poll,
            &mut events,
            vec![ExpectEvent::new(UDATA, Interest::LIO)],
        );
    }
}

redpajama_set_name: RedPajamaGithub


text:
//
// Copyright 2013-2014 Hans Wolff
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

using RedFoxMQ.Transports;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace RedFoxMQ
{
    [DebuggerDisplay("MessageFrameQueueWriter: {_queueSocket}")]
    class MessageFrameQueueWriter : IMessageFrameWriter
    {
        private readonly IQueueSocket _queueSocket;
        public MessageFrameQueueWriter(IQueueSocket queueSocket)
        {
            if (queueSocket == null) throw new ArgumentNullException("queueSocket");
            _queueSocket = queueSocket;
        }

        public void WriteMessageFrame(MessageFrame messageFrame)
        {
            if (messageFrame == null) throw new ArgumentNullException("messageFrame");
            if (messageFrame.RawMessage == null) throw new ArgumentException("messageFrame.RawMessage cannot be null");

            _queueSocket.Write(messageFrame);
        }

        public async Task WriteMessageFrameAsync(MessageFrame messageFrame, CancellationToken cancellationToken)
        {
            if (messageFrame == null) throw new ArgumentNullException("messageFrame");
            if (messageFrame.RawMessage == null) throw new ArgumentException("messageFrame.RawMessage cannot be null");

            _queueSocket.Write(messageFrame);
        }

        public void WriteMessageFrames(ICollection<MessageFrame> messageFrames)
        {
            if (messageFrames == null) return;

            foreach (var messageFrame in messageFrames)
            {
                _queueSocket.Write(messageFrame);
            }
        }

        public async Task WriteMessageFramesAsync(ICollection<MessageFrame> messageFrames, CancellationToken cancellationToken)
        {
            if (messageFrames == null) return;

            foreach (var messageFrame in messageFrames)
            {
                _queueSocket.Write(messageFrame);
            }
        }
    }
}


text:
package net.smalinuxer.rebot.frame;

public class ReactorShutDownException extends Exception {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	public ReactorShutDownException() {
		super("Reactor have already shutdown !");
	}

	public ReactorShutDownException(String message, Throwable cause,
			boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
	}

	public ReactorShutDownException(String message, Throwable cause) {
		super(message, cause);
	}

	public ReactorShutDownException(String message) {
		super(message);
	}

	public ReactorShutDownException(Throwable cause) {
		super(cause);
	}

}


text:
package com.enigmabridge;

/**
 * References EB engine.
 * Created by dusanklinec on 27.04.16.
 */
public interface EBEngineReference {
    EBEngine getEBEngine();
}


text:
const int max_thread_count = 8;
const int loops = 100000;

const char* my_default = "Hallo Welt!";

const std::size_t upper_limit = 2500;
const std::size_t lower_limit = 1000;

typedef char charT;

typedef std::string String;

typedef String MyType;

void*
thread_main (void*)
{
  typedef std::map<unsigned int,MyType> Map;
  typedef Map::value_type Value_Pair;
  Map myMap;

  for (int loop = 0; loop < loops; loop++)
    {
      String& str = myMap[loop];
      str.append (my_default);
      myMap.insert (Value_Pair (loop, str));

      if (myMap.size () > upper_limit)
	{
	  while (myMap.size () > lower_limit)
	    {
	      Map::iterator it = myMap.begin ();
	      myMap.erase (it);
	    }
	}
    }

  return 0;
}

int
main (void)
{
  pthread_t tid[max_thread_count];

#if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
  pthread_setconcurrency (max_thread_count);
#endif

  for (int i = 0; i < max_thread_count; i++)
    pthread_create (&tid[i], NULL, thread_main, 0);

  for (int i = 0; i < max_thread_count; i++)
    pthread_join (tid[i], NULL);

  return 0;
}


text:
use crate::algorithm::Printer;
use crate::iter::IterDelimited;
use crate::INDENT;
use proc_macro2::TokenStream;
use syn::{
    Fields, FnArg, ForeignItem, ForeignItemFn, ForeignItemMacro, ForeignItemStatic,
    ForeignItemType, ImplItem, ImplItemConst, ImplItemMacro, ImplItemMethod, ImplItemType, Item,
    ItemConst, ItemEnum, ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl, ItemMacro, ItemMacro2,
    ItemMod, ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType, ItemUnion, ItemUse, Pat,
    Receiver, Signature, Stmt, TraitItem, TraitItemConst, TraitItemMacro, TraitItemMethod,
    TraitItemType, Type, UseGlob, UseGroup, UseName, UsePath, UseRename, UseTree,
};

impl Printer {
    pub fn item(&mut self, item: &Item) {
        match item {
            Item::Const(item) => self.item_const(item),
            Item::Enum(item) => self.item_enum(item),
            Item::ExternCrate(item) => self.item_extern_crate(item),
            Item::Fn(item) => self.item_fn(item),
            Item::ForeignMod(item) => self.item_foreign_mod(item),
            Item::Impl(item) => self.item_impl(item),
            Item::Macro(item) => self.item_macro(item),
            Item::Macro2(item) => self.item_macro2(item),
            Item::Mod(item) => self.item_mod(item),
            Item::Static(item) => self.item_static(item),
            Item::Struct(item) => self.item_struct(item),
            Item::Trait(item) => self.item_trait(item),
            Item::TraitAlias(item) => self.item_trait_alias(item),
            Item::Type(item) => self.item_type(item),
            Item::Union(item) => self.item_union(item),
            Item::Use(item) => self.item_use(item),
            Item::Verbatim(item) => self.item_verbatim(item),
            #[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
            _ => unimplemented!("unknown Item"),
        }
    }

    fn item_const(&mut self, item: &ItemConst) {
        self.outer_attrs(&item.attrs);
        self.cbox(0);
        self.visibility(&item.vis);
        self.word("const ");
        self.ident(&item.ident);
        self.word(": ");
        self.ty(&item.ty);
        self.word(" = ");
        self.neverbreak();
        self.expr(&item.expr);
        self.word(";");
        self.end();
        self.hardbreak();
    }

    fn item_enum(&mut self, item: &ItemEnum) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.visibility(&item.vis);
        self.word("enum ");
        self.ident(&item.ident);
        self.generics(&item.generics);
        self.where_clause_for_body(&item.generics.where_clause);
        self.word("{");
        self.hardbreak_if_nonempty();
        for variant in &item.variants {
            self.variant(variant);
            self.word(",");
            self.hardbreak();
        }
        self.offset(-INDENT);
        self.end();
        self.word("}");
        self.hardbreak();
    }

    fn item_extern_crate(&mut self, item: &ItemExternCrate) {
        self.outer_attrs(&item.attrs);
        self.visibility(&item.vis);
        self.word("extern crate ");
        self.ident(&item.ident);
        if let Some((_as_token, rename)) = &item.rename {
            self.word(" as ");
            self.ident(rename);
        }
        self.word(";");
        self.hardbreak();
    }

    fn item_fn(&mut self, item: &ItemFn) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.visibility(&item.vis);
        self.signature(&item.sig);
        self.where_clause_for_body(&item.sig.generics.where_clause);
        self.word("{");
        self.hardbreak_if_nonempty();
        self.inner_attrs(&item.attrs);
        for stmt in &item.block.stmts {
            self.stmt(stmt);
        }
        self.offset(-INDENT);
        self.end();
        self.word("}");
        self.hardbreak();
    }

    fn item_foreign_mod(&mut self, item: &ItemForeignMod) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.abi(&item.abi);
        self.word("{");
        self.hardbreak_if_nonempty();
        self.inner_attrs(&item.attrs);
        for foreign_item in &item.items {
            self.foreign_item(foreign_item);
        }
        self.offset(-INDENT);
        self.end();
        self.word("}");
        self.hardbreak();
    }

    fn item_impl(&mut self, item: &ItemImpl) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.ibox(-INDENT);
        self.cbox(INDENT);
        if item.defaultness.is_some() {
            self.word("default ");
        }
        if item.unsafety.is_some() {
            self.word("unsafe ");
        }
        self.word("impl");
        self.generics(&item.generics);
        self.end();
        self.nbsp();
        if let Some((negative_polarity, path, _for_token)) = &item.trait_ {
            if negative_polarity.is_some() {
                self.word("!");
            }
            self.path(path);
            self.space();
            self.word("for ");
        }
        self.ty(&item.self_ty);
        self.end();
        self.where_clause_for_body(&item.generics.where_clause);
        self.word("{");
        self.hardbreak_if_nonempty();
        self.inner_attrs(&item.attrs);
        for impl_item in &item.items {
            self.impl_item(impl_item);
        }
        self.offset(-INDENT);
        self.end();
        self.word("}");
        self.hardbreak();
    }

    fn item_macro(&mut self, item: &ItemMacro) {
        self.outer_attrs(&item.attrs);
        self.mac(&item.mac, item.ident.as_ref());
        self.mac_semi_if_needed(&item.mac.delimiter);
        self.hardbreak();
    }

    fn item_macro2(&mut self, item: &ItemMacro2) {
        unimplemented!("Item::Macro2 `macro {} {}`", item.ident, item.rules);
    }

    fn item_mod(&mut self, item: &ItemMod) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.visibility(&item.vis);
        self.word("mod ");
        self.ident(&item.ident);
        if let Some((_brace, items)) = &item.content {
            self.word(" {");
            self.hardbreak_if_nonempty();
            self.inner_attrs(&item.attrs);
            for item in items {
                self.item(item);
            }
            self.offset(-INDENT);
            self.end();
            self.word("}");
        } else {
            self.word(";");
            self.end();
        }
        self.hardbreak();
    }

    fn item_static(&mut self, item: &ItemStatic) {
        self.outer_attrs(&item.attrs);
        self.cbox(0);
        self.visibility(&item.vis);
        self.word("static ");
        if item.mutability.is_some() {
            self.word("mut ");
        }
        self.ident(&item.ident);
        self.word(": ");
        self.ty(&item.ty);
        self.word(" = ");
        self.neverbreak();
        self.expr(&item.expr);
        self.word(";");
        self.end();
        self.hardbreak();
    }

    fn item_struct(&mut self, item: &ItemStruct) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.visibility(&item.vis);
        self.word("struct ");
        self.ident(&item.ident);
        self.generics(&item.generics);
        match &item.fields {
            Fields::Named(fields) => {
                self.where_clause_for_body(&item.generics.where_clause);
                self.word("{");
                self.hardbreak_if_nonempty();
                for field in &fields.named {
                    self.field(field);
                    self.word(",");
                    self.hardbreak();
                }
                self.offset(-INDENT);
                self.end();
                self.word("}");
            }
            Fields::Unnamed(fields) => {
                self.fields_unnamed(fields);
                self.where_clause_semi(&item.generics.where_clause);
                self.end();
            }
            Fields::Unit => {
                self.where_clause_semi(&item.generics.where_clause);
                self.end();
            }
        }
        self.hardbreak();
    }

    fn item_trait(&mut self, item: &ItemTrait) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.visibility(&item.vis);
        if item.unsafety.is_some() {
            self.word("unsafe ");
        }
        if item.auto_token.is_some() {
            self.word("auto ");
        }
        self.word("trait ");
        self.ident(&item.ident);
        self.generics(&item.generics);
        for supertrait in item.supertraits.iter().delimited() {
            if supertrait.is_first {
                self.word(": ");
            } else {
                self.word(" + ");
            }
            self.type_param_bound(&supertrait);
        }
        self.where_clause_for_body(&item.generics.where_clause);
        self.word("{");
        self.hardbreak_if_nonempty();
        self.inner_attrs(&item.attrs);
        for trait_item in &item.items {
            self.trait_item(trait_item);
        }
        self.offset(-INDENT);
        self.end();
        self.word("}");
        self.hardbreak();
    }

    fn item_trait_alias(&mut self, item: &ItemTraitAlias) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.visibility(&item.vis);
        self.word("trait ");
        self.ident(&item.ident);
        self.generics(&item.generics);
        self.word(" = ");
        self.neverbreak();
        for bound in item.bounds.iter().delimited() {
            if !bound.is_first {
                self.space();
                self.word("+ ");
            }
            self.type_param_bound(&bound);
        }
        self.where_clause_semi(&item.generics.where_clause);
        self.end();
        self.hardbreak();
    }

    fn item_type(&mut self, item: &ItemType) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.visibility(&item.vis);
        self.word("type ");
        self.ident(&item.ident);
        self.generics(&item.generics);
        self.where_clause_oneline(&item.generics.where_clause);
        self.word("= ");
        self.neverbreak();
        self.ibox(-INDENT);
        self.ty(&item.ty);
        self.end();
        self.word(";");
        self.end();
        self.hardbreak();
    }

    fn item_union(&mut self, item: &ItemUnion) {
        self.outer_attrs(&item.attrs);
        self.cbox(INDENT);
        self.visibility(&item.vis);
        self.word("union ");
        self.ident(&item.ident);
        self.generics(&item.generics);
        self.where_clause_for_body(&item.generics.where_clause);
        self.word("{");
        self.hardbreak_if_nonempty();
        for field in &item.fields.named {
            self.field(field);
            self.word(",");
            self.hardbreak();
        }
        self.offset(-INDENT);
        self.end();
        self.word("}");
        self.hardbreak();
    }

    fn item_use(&mut self, item: &ItemUse) {
        self.outer_attrs(&item.attrs);
        self.visibility(&item.vis);
        self.word("use ");
        if item.leading_colon.is_some() {
            self.word("::");
        }
        self.use_tree(&item.tree);
        self.word(";");
        self.hardbreak();
    }

    #[cfg(not(feature = "verbatim"))]
    fn item_verbatim(&mut self, item: &TokenStream) {
        if !item.is_empty() {
            unimplemented!("Item::Verbatim `{}`", item);
        }
        self.hardbreak();
    }

    #[cfg(feature = "verbatim")]
    fn item_verbatim(&mut self, tokens: &TokenStream) {
        use syn::parse::{Parse, ParseStream, Result};
        use syn::{Attribute, Token};

        enum ItemVerbatim {
            Empty,
            UnsafeForeignMod(ItemForeignMod),
        }

        impl Parse for ItemVerbatim {
            fn parse(input: ParseStream) -> Result<Self> {
                if input.is_empty() {
                    Ok(ItemVerbatim::Empty)
                } else {
                    let attrs = input.call(Attribute::parse_outer)?;
                    input.parse::<Token![unsafe]>()?;
                    let module: ItemForeignMod = input.parse()?;
                    Ok(ItemVerbatim::UnsafeForeignMod(ItemForeignMod {
                        attrs,
                        ..module
                    }))
                }
            }
        }

        let item: ItemVerbatim = match syn::parse2(tokens.clone()) {
            Ok(item) => item,
            Err(_) => unimplemented!("Item::Verbatim `{}`", tokens),
        };

        match item {
            ItemVerbatim::Empty => {}
            ItemVerbatim::UnsafeForeignMod(item) => {
                self.outer_attrs(&item.attrs);
                self.cbox(INDENT);
                self.word("unsafe ");
                self.abi(&item.abi);
                self.word("{");
                self.hardbreak_if_nonempty();
                self.inner_attrs(&item.attrs);
                for foreign_item in &item.items {
                    self.foreign_item(foreign_item);
                }
                self.offset(-INDENT);
                self.end();
                self.word("}");
            }
        }

        self.hardbreak();
    }

    fn use_tree(&mut self, use_tree: &UseTree) {
        match use_tree {
            UseTree::Path(use_path) => self.use_path(use_path),
            UseTree::Name(use_name) => self.use_name(use_name),
            UseTree::Rename(use_rename) => self.use_rename(use_rename),
            UseTree::Glob(use_glob) => self.use_glob(use_glob),
            UseTree::Group(use_group) => self.use_group(use_group),
        }
    }

    fn use_path(&mut self, use_path: &UsePath) {
        self.ident(&use_path.ident);
        self.word("::");
        self.use_tree(&use_path.tree);
    }

    fn use_name(&mut self, use_name: &UseName) {
        self.ident(&use_name.ident);
    }

    fn use_rename(&mut self, use_rename: &UseRename) {
        self.ident(&use_rename.ident);
        self.word(" as ");
        self.ident(&use_rename.rename);
    }

    fn use_glob(&mut self, use_glob: &UseGlob) {
        let _ = use_glob;
        self.word("*");
    }

    fn use_group(&mut self, use_group: &UseGroup) {
        if use_group.items.is_empty() {
            self.word("{}");
        } else if use_group.items.len() == 1 {
            self.use_tree(&use_group.items[0]);
        } else {
            self.cbox(INDENT);
            self.word("{");
            self.zerobreak();
            self.ibox(0);
            for use_tree in use_group.items.iter().delimited() {
                self.use_tree(&use_tree);
                if !use_tree.is_last {
                    self.word(",");
                    let mut use_tree = *use_tree;
                    while let UseTree::Path(use_path) = use_tree {
                        use_tree = &use_path.tree;
                    }
                    if let UseTree::Group(_) = use_tree {
                        self.hardbreak();
                    } else {
                        self.space();
                    }
                }
            }
            self.end();
            self.trailing_comma(true);
            self.offset(-INDENT);
            self.word("}");
            self.end();
        }
    }

    fn foreign_item(&mut self, foreign_item: &ForeignItem) {
        match foreign_item {
            ForeignItem::Fn(item) => self.foreign_item_fn(item),
            ForeignItem::Static(item) => self.foreign_item_static(item),
            ForeignItem::Type(item) => self.foreign_item_type(item),
            ForeignItem::Macro(item) => self.foreign_item_macro(item),
            ForeignItem::Verbatim(item) => self.foreign_item_verbatim(item),
            #[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
            _ => unimplemented!("unknown ForeignItem"),
        }
    }

    fn foreign_item_fn(&mut self, foreign_item: &ForeignItemFn) {
        self.outer_attrs(&foreign_item.attrs);
        self.cbox(INDENT);
        self.visibility(&foreign_item.vis);
        self.signature(&foreign_item.sig);
        self.where_clause_semi(&foreign_item.sig.generics.where_clause);
        self.end();
        self.hardbreak();
    }

    fn foreign_item_static(&mut self, foreign_item: &ForeignItemStatic) {
        self.outer_attrs(&foreign_item.attrs);
        self.cbox(0);
        self.visibility(&foreign_item.vis);
        self.word("static ");
        if foreign_item.mutability.is_some() {
            self.word("mut ");
        }
        self.ident(&foreign_item.ident);
        self.word(": ");
        self.ty(&foreign_item.ty);
        self.word(";");
        self.end();
        self.hardbreak();
    }

    fn foreign_item_type(&mut self, foreign_item: &ForeignItemType) {
        self.outer_attrs(&foreign_item.attrs);
        self.cbox(0);
        self.visibility(&foreign_item.vis);
        self.word("type ");
        self.ident(&foreign_item.ident);
        self.word(";");
        self.end();
        self.hardbreak();
    }

    fn foreign_item_macro(&mut self, foreign_item: &ForeignItemMacro) {
        self.outer_attrs(&foreign_item.attrs);
        self.mac(&foreign_item.mac, None);
        self.mac_semi_if_needed(&foreign_item.mac.delimiter);
        self.hardbreak();
    }

    #[cfg(not(feature = "verbatim"))]
    fn foreign_item_verbatim(&mut self, foreign_item: &TokenStream) {
        if !foreign_item.is_empty() {
            unimplemented!("ForeignItem::Verbatim `{}`", foreign_item);
        }
        self.hardbreak();
    }

    #[cfg(feature = "verbatim")]
    fn foreign_item_verbatim(&mut self, tokens: &TokenStream) {
        use syn::parse::{Parse, ParseStream, Result};

        enum ForeignItemVerbatim {
            TypeAlias(ItemType),
        }

        impl Parse for ForeignItemVerbatim {
            fn parse(input: ParseStream) -> Result<Self> {
                input.parse().map(ForeignItemVerbatim::TypeAlias)
            }
        }

        let foreign_item: ForeignItemVerbatim = match syn::parse2(tokens.clone()) {
            Ok(foreign_item) => foreign_item,
            Err(_) => unimplemented!("ForeignItem::Verbatim `{}`", tokens),
        };

        match foreign_item {
            ForeignItemVerbatim::TypeAlias(item) => self.item_type(&item),
        }
    }

    fn trait_item(&mut self, trait_item: &TraitItem) {
        match trait_item {
            TraitItem::Const(item) => self.trait_item_const(item),
            TraitItem::Method(item) => self.trait_item_method(item),
            TraitItem::Type(item) => self.trait_item_type(item),
            TraitItem::Macro(item) => self.trait_item_macro(item),
            TraitItem::Verbatim(item) => self.trait_item_verbatim(item),
            #[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
            _ => unimplemented!("unknown TraitItem"),
        }
    }

    fn trait_item_const(&mut self, trait_item: &TraitItemConst) {
        self.outer_attrs(&trait_item.attrs);
        self.cbox(0);
        self.word("const ");
        self.ident(&trait_item.ident);
        self.word(": ");
        self.ty(&trait_item.ty);
        if let Some((_eq_token, default)) = &trait_item.default {
            self.word(" = ");
            self.neverbreak();
            self.expr(default);
        }
        self.word(";");
        self.end();
        self.hardbreak();
    }

    fn trait_item_method(&mut self, trait_item: &TraitItemMethod) {
        self.outer_attrs(&trait_item.attrs);
        self.cbox(INDENT);
        self.signature(&trait_item.sig);
        if let Some(block) = &trait_item.default {
            self.where_clause_for_body(&trait_item.sig.generics.where_clause);
            self.word("{");
            self.hardbreak_if_nonempty();
            self.inner_attrs(&trait_item.attrs);
            for stmt in &block.stmts {
                self.stmt(stmt);
            }
            self.offset(-INDENT);
            self.end();
            self.word("}");
        } else {
            self.where_clause_semi(&trait_item.sig.generics.where_clause);
            self.end();
        }
        self.hardbreak();
    }

    fn trait_item_type(&mut self, trait_item: &TraitItemType) {
        self.outer_attrs(&trait_item.attrs);
        self.cbox(INDENT);
        self.word("type ");
        self.ident(&trait_item.ident);
        self.generics(&trait_item.generics);
        for bound in trait_item.bounds.iter().delimited() {
            if bound.is_first {
                self.word(": ");
            } else {
                self.space();
                self.word("+ ");
            }
            self.type_param_bound(&bound);
        }
        if let Some((_eq_token, default)) = &trait_item.default {
            self.where_clause_oneline(&trait_item.generics.where_clause);
            self.word("= ");
            self.neverbreak();
            self.ty(default);
        } else {
            self.where_clause_oneline_semi(&trait_item.generics.where_clause);
        }
        self.end();
        self.hardbreak();
    }

    fn trait_item_macro(&mut self, trait_item: &TraitItemMacro) {
        self.outer_attrs(&trait_item.attrs);
        self.mac(&trait_item.mac, None);
        self.mac_semi_if_needed(&trait_item.mac.delimiter);
        self.hardbreak();
    }

    fn trait_item_verbatim(&mut self, trait_item: &TokenStream) {
        if !trait_item.is_empty() {
            unimplemented!("TraitItem::Verbatim `{}`", trait_item);
        }
        self.hardbreak();
    }

    fn impl_item(&mut self, impl_item: &ImplItem) {
        match impl_item {
            ImplItem::Const(item) => self.impl_item_const(item),
            ImplItem::Method(item) => self.impl_item_method(item),
            ImplItem::Type(item) => self.impl_item_type(item),
            ImplItem::Macro(item) => self.impl_item_macro(item),
            ImplItem::Verbatim(item) => self.impl_item_verbatim(item),
            #[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
            _ => unimplemented!("unknown ImplItem"),
        }
    }

    fn impl_item_const(&mut self, impl_item: &ImplItemConst) {
        self.outer_attrs(&impl_item.attrs);
        self.cbox(0);
        self.visibility(&impl_item.vis);
        if impl_item.defaultness.is_some() {
            self.word("default ");
        }
        self.word("const ");
        self.ident(&impl_item.ident);
        self.word(": ");
        self.ty(&impl_item.ty);
        self.word(" = ");
        self.neverbreak();
        self.expr(&impl_item.expr);
        self.word(";");
        self.end();
        self.hardbreak();
    }

    fn impl_item_method(&mut self, impl_item: &ImplItemMethod) {
        self.outer_attrs(&impl_item.attrs);
        self.cbox(INDENT);
        self.visibility(&impl_item.vis);
        if impl_item.defaultness.is_some() {
            self.word("default ");
        }
        self.signature(&impl_item.sig);
        if impl_item.block.stmts.len() == 1 {
            if let Stmt::Item(Item::Verbatim(verbatim)) = &impl_item.block.stmts[0] {
                if verbatim.to_string() == ";" {
                    self.where_clause_semi(&impl_item.sig.generics.where_clause);
                    self.end();
                    self.hardbreak();
                    return;
                }
            }
        }
        self.where_clause_for_body(&impl_item.sig.generics.where_clause);
        self.word("{");
        self.hardbreak_if_nonempty();
        self.inner_attrs(&impl_item.attrs);
        for stmt in &impl_item.block.stmts {
            self.stmt(stmt);
        }
        self.offset(-INDENT);
        self.end();
        self.word("}");
        self.hardbreak();
    }

    fn impl_item_type(&mut self, impl_item: &ImplItemType) {
        self.outer_attrs(&impl_item.attrs);
        self.cbox(INDENT);
        self.visibility(&impl_item.vis);
        if impl_item.defaultness.is_some() {
            self.word("default ");
        }
        self.word("type ");
        self.ident(&impl_item.ident);
        self.generics(&impl_item.generics);
        self.where_clause_oneline(&impl_item.generics.where_clause);
        self.word("= ");
        self.neverbreak();
        self.ibox(-INDENT);
        self.ty(&impl_item.ty);
        self.end();
        self.word(";");
        self.end();
        self.hardbreak();
    }

    fn impl_item_macro(&mut self, impl_item: &ImplItemMacro) {
        self.outer_attrs(&impl_item.attrs);
        self.mac(&impl_item.mac, None);
        self.mac_semi_if_needed(&impl_item.mac.delimiter);
        self.hardbreak();
    }

    fn impl_item_verbatim(&mut self, impl_item: &TokenStream) {
        if !impl_item.is_empty() {
            unimplemented!("ImplItem::Verbatim `{}`", impl_item);
        }
        self.hardbreak();
    }

    fn maybe_variadic(&mut self, arg: &FnArg) -> bool {
        let pat_type = match arg {
            FnArg::Typed(pat_type) => pat_type,
            FnArg::Receiver(receiver) => {
                self.receiver(receiver);
                return false;
            }
        };

        match pat_type.ty.as_ref() {
            Type::Verbatim(ty) if ty.to_string() == "..." => {
                match pat_type.pat.as_ref() {
                    Pat::Verbatim(pat) if pat.to_string() == "..." => {
                        self.outer_attrs(&pat_type.attrs);
                        self.word("...");
                    }
                    _ => self.pat_type(pat_type),
                }
                true
            }
            _ => {
                self.pat_type(pat_type);
                false
            }
        }
    }

    fn signature(&mut self, signature: &Signature) {
        if signature.constness.is_some() {
            self.word("const ");
        }
        if signature.asyncness.is_some() {
            self.word("async ");
        }
        if signature.unsafety.is_some() {
            self.word("unsafe ");
        }
        if let Some(abi) = &signature.abi {
            self.abi(abi);
        }
        self.word("fn ");
        self.ident(&signature.ident);
        self.generics(&signature.generics);
        self.word("(");
        self.neverbreak();
        self.cbox(0);
        self.zerobreak();
        let mut last_is_variadic = false;
        for input in signature.inputs.iter().delimited() {
            last_is_variadic = self.maybe_variadic(&input);
            if last_is_variadic {
                self.zerobreak();
            } else {
                self.trailing_comma(input.is_last);
            }
        }
        if signature.variadic.is_some() && !last_is_variadic {
            self.word("...");
            self.zerobreak();
        }
        self.offset(-INDENT);
        self.end();
        self.word(")");
        self.cbox(-INDENT);
        self.return_type(&signature.output);
        self.end();
    }

    fn receiver(&mut self, receiver: &Receiver) {
        self.outer_attrs(&receiver.attrs);
        if let Some((_ampersand, lifetime)) = &receiver.reference {
            self.word("&");
            if let Some(lifetime) = lifetime {
                self.lifetime(lifetime);
                self.nbsp();
            }
        }
        if receiver.mutability.is_some() {
            self.word("mut ");
        }
        self.word("self");
    }
}


text:
'use strict';

var fs = require('fs');
var path = require('path');
var yaml = require('js-yaml');
var out = require('./out');
var args = require('./args');

var options = [{
  name: 'admin',
  flag: 'a',
  param: 'port',
  default: 8889,
  description: 'Port for admin portal. Defaults to 8889.'
}, {
  name: 'cert',
  flag: 'c',
  param: 'file',
  default: __dirname + '/../../tls/cert.pem',
  description: 'Certificate file. Use with --key.'
}, {
  name: 'data',
  flag: 'd',
  param: 'file',
  description: 'Data file to pre-load endoints. YAML or JSON format.'
}, {
  name: 'help',
  flag: 'h',
  default: false,
  description: 'This help text.'
}, {
  name: 'key',
  flag: 'k',
  param: 'file',
  default: __dirname + '/../../tls/key.pem',
  description: 'Private key file. Use with --cert.'
}, {
  name: 'location',
  flag: 'l',
  param: 'hostname',
  default: '0.0.0.0',
  description: 'Hostname at which to bind stubby.'
}, {
  name: 'mute',
  flag: 'm',
  description: 'Prevent stubby from printing to the console.'
}, {
  name: 'pfx',
  flag: 'p',
  param: 'file',
  description: 'PFX file. Ignored if used with --key/--cert'
}, {
  name: 'stubs',
  flag: 's',
  param: 'port',
  default: 8882,
  description: 'Port for stubs portal. Defaults to 8882.'
}, {
  name: 'tls',
  flag: 't',
  param: 'port',
  default: 7443,
  description: 'Port for https stubs portal. Defaults to 7443.'
}, {
  name: 'version',
  flag: 'v',
  description: "Prints stubby's version number."
}, {
  name: 'watch',
  flag: 'w',
  description: 'Auto-reload data file when edits are made.'
}];

function help(go) {
  if (go == null) { go = false; }
  if (!go) { return; }

  out.log(args.helpText(options, 'stubby'));
  process.exit();
}

function version(go) {
  var ver = (require('../../package.json')).version;

  if (!go) { return ver; }

  out.log(ver);
  process.exit();
}

function data(filename) {
  var filedata;

  if (filename === null) { return []; }

  try {
    filedata = (fs.readFileSync(filename, 'utf8')).trim();
  } catch (e) {
    out.warn('File "' + filename + '" could not be found. Ignoring...');
    return [];
  }

  try {
    return yaml.load(filedata);
  } catch (e) {
    out.warn('Couldn\'t parse "' + filename + '" due to syntax errors:');
    out.log(e.message);
    process.exit(0);
  }
}

function key(file) { return readFile('k', 'key', file, 'pem'); }
function cert(file) { return readFile('c', 'cert', file, 'pem'); }
function pfx(file) { return readFile('p', 'pfx', file, 'pfx'); }

function readFile(flag, option, filename, type) {
  var filedata, extension;

  if (filename === null) { return null; }

  filedata = fs.readFileSync(filename, 'utf8');
  extension = filename.replace(/^.*\.([a-zA-Z0-9]+)$/, '$1');

  if (!filedata) { return null; }

  if (extension !== type) {
    out.warn('[-' + flag + ', --' + option + '] only takes files of type .' + type + '. Ignoring...');
    return null;
  }

  return filedata.trim();
}

function getArgs(argv) {
  var self = this; // eslint-disable-line
  var params;

  if (argv == null) { argv = process.argv; }

  params = args.parse(options, argv);
  params.datadir = path.resolve(path.dirname(params.data));

  if (params.watch) { params.watch = params.data; }

  options.forEach(function (option) {
    if (self[option.name] != null) {
      params[option.name] = self[option.name](params[option.name]);
    }
  });

  return params;
}

module.exports = {
  options: options,
  help: help,
  version: version,
  data: data,
  key: key,
  cert: cert,
  pfx: pfx,
  readFile: readFile,
  getArgs: getArgs
};